Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 966893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:13:55+00:00 2026-05-16T02:13:55+00:00

I’ve got a function that appends a <div> to an element on click. The

  • 0

I’ve got a function that appends a <div> to an element on click. The function gets the text of the clicked element and assigns it to a variable called name. That variable is then used as the <div> id of the appended element.

I need to see if a <div> id with name already exists before I append the element but I don’t know how to find this.

Here is my code:

$("li.friend").live('click', function() {
  name = $(this).text();

  // if-statement checking for existence of <div> should go here
  // If <div> does not exist, then append element
    $("div#chatbar").append("<div class='labels'><div id='" + name + "' style='display:none;'></div>" + name + "</div>");

  // Else
    alert('this record already exists');
});

This seems pretty straightforward but I’m getting the error “Unexpected end of file while searching for class name”. I have no clue what that means.

if (document.getElementById(name)) {
  $("div#" + name).css({bottom: '30px'});
} else {
  $("div#page-content div#chatbar").append("<div class='labels'>" + name + "</div><div id='" + name + "'></div>");
}

What’s more is that I want to be able to delete this element if I close it out which should then remove the div id [name] from the document but .remove() does not do this.

Here is the code for that:

$(".mini-close").live('click', function(){
  $(this).parent().remove();
});

I added .mini-close to the append function as a child of .labels so there was a way to close out of the appended <div> if needed. After clicking .mini-close and attempting to click the same name again from li.friends it still finds the div id [name] and returns the first part of my if statement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:13:55+00:00Added an answer on May 16, 2026 at 2:13 am

    You can use .length after the selector to see if it matched any elements, like this:

    if($("#" + name).length == 0) {
      //it doesn't exist
    }
    

    The full version:

    $("li.friend").live('click', function(){
      name = $(this).text();
      if($("#" + name).length == 0) {
        $("div#chatbar").append("<div class='labels'><div id='" + name + "' style='display:none;'></div>" + name + "</div>");
      } else {
        alert('this record already exists');
      }
    });
    

    Or, the non-jQuery version for this part (since it’s an ID):

    $("li.friend").live('click', function(){
      name = $(this).text();
      if(document.getElementById(name) == null) {
        $("div#chatbar").append("<div class='labels'><div id='" + name + "' style='display:none;'></div>" + name + "</div>");
      } else {
        alert('this record already exists');
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a function that works when called in IE and Chrome, but it
I've got some maths being calculated that I then output in a div in
I've got a function that uses readline to ask people to enter in data.
I've got a function that takes two strings as arguments, and I want to
I've got a function that does (in short): my $file = IO::File->new(| some_command >>
I've got a jQuery function that triggers when the selected option from a select
I've got a Matlab function that takes some time to run, and I'd like
I've got a function here that is meant to look through a list of
I've got one function checkEvery15Seconds that runs every 15 seconds. It checks to see
I've got a view function that has to decide which form to use depending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.