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'); } });
Related Posts:
- How do I redirect to another webpage?
- Uncaught ReferenceError: jQuery is not defined [duplicate]
- How do I check whether a checkbox is checked in jQuery?
- syntax error: unexpected token <
- Error OPTIONS net::ERR_CONNECTION_REFUSED
- Uncaught ReferenceError: jQuery is not defined [duplicate]
- JQuery – $ is not defined
- Cannot read property ‘push’ of undefined when combining arrays
- Cannot read property ‘push’ of undefined when combining arrays
- Download File Using JavaScript/jQuery
- Disabling and enabling a html input button
- Check if checkbox is checked with jQuery
- How can I get the data-id attribute?
- document.getElementById vs jQuery $()
- Check if checkbox is checked with jQuery
- How to import jquery using ES6 syntax?
- How to filter an array/object by checking multiple values
- Uncaught SyntaxError: Invalid or unexpected token
- Infinite Scrolling in Asp.Net MVC with jQuery / AJAX Issues
- What are the difference between $(document).bind(‘ready’, function) and $(document).ready(function() {})
- How can jQuery deferred be used?
- Uncaught TypeError : cannot read property ‘replace’ of undefined In Grid
- Disable/enable an input with jQuery?
- How to use php code in javascript
- Adding images to a Bootstrap Carousel with AJAX
- How to read xml file contents in jQuery and display in html elements?
- How can I print using JQuery
- Unable to preventDefault inside passive event listener
- Loading local JSON file
- jQuery $.cookie is not a function
- How to check all checkboxes using jQuery?
- Setting background-image using jQuery CSS property
- ChangeDate – Date Picker Bootstrap
- Uncaught TypeError: Cannot read property ‘msie’ of undefined – jQuery tools
- Scroll Automatically to the Bottom of the Page
- How do I check if string contains substring?
- How can I select an element by name with jQuery?
- keycode 13 is for which key
- How to update array value javascript?
- jQuery disable/enable submit button
- Equivalent of jQuery .hide() to set visibility: hidden
- How can I get the last character in a string?
- Uncaught TypeError: Cannot read property ‘name’ of undefined
- What does window.jQuery and window.$ mean?
- Dynamically load JS inside JS
- Simulate Keypress With jQuery
- JQuery issue “TypeError: $.getJSON is not a function”
- Adding options to a
- How to add an object to an array
- jQuery Ajax POST example with PHP
- Refresh/reload the content in Div using jquery/ajax
- Convert CSV data into JSON format using Javascript
- this vs $(this)
- How to open a URL in a new Tab using JavaScript or jQuery?
- SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- HTML Entity Decode
- Extract hostname name from string
- jquery beforeunload when closing (not leaving) the page?
- how to get the value of a textarea in jquery?
- How do I pass the this context to a function?
- Escaping HTML strings with jQuery
- Why define an anonymous function and pass it jQuery as the argument?
- How to scroll an HTML page to a given anchor
- How can I get form data with JavaScript/jQuery?
- JQuery get the title of a button
- Trying to load jquery into tampermonkey script
- Preloading images with jQuery
- How to fire an event on class change using jQuery?
- Uncaught SyntaxError: Unexpected token with JSON.parse
- TypeError getElementsByTagName is not a function issue
- How to open a Bootstrap modal window using jQuery?
- window.location.replace() not working to redirect browser
- How to check if the URL contains a given string?
- Check if url contains string with JQuery
- Download a file by jQuery.Ajax
- stopPropagation vs. stopImmediatePropagation
- What’s a Good Javascript Time Picker?
- Ajax success function
- How can I exit from a javascript function? [duplicate]
- Cannot read property ‘preventDefault’ of undefined in javascript error
- Object doesn’t support property or method ‘addEventListener’
- How to fix javascript .toFixed is not a Function error
- jQuery checkbox checked state changed event
- etect the Enter key in a text input field
- How to set “style=display:none;” using jQuery’s attr method?
- WordPress 5.5 – ReferenceError: commonL10n is not defined error
- Jquery function working in Chrome Console but not in the code [closed]
- List of JS Events for Widgets?
- How to set callback to execute after all the tinyMCE editors have been initialized?
- How do I make my wordpress post appear in a mouse hover preview?
- Uncaught ReferenceError: jQuery is not defined – although jQuery is loaded [closed]
- Change each text after 4 second and each image after 2 second
- wp post_tag change minlength
- How to locate the js code and php code for certain function?
- Open image gallery on link click
- Use one javascript variable into another javascript file
- Uncaught ReferenceError: tippy is not defined
- Error – Uncaught TypeError: e.dispatchEvent is not a function
- Styling parent based on a child element of a child in a WordPress page