I’ve got a function that appends a
Here is the jQuery function I use:
function isExists(var elemId){
return jQuery('#'+elemId).length > 0;
}
This will return a boolean value. If element exists, it returns true.
If you want to select element by class name, just replace #
with .
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');
}
});
You can check by using jquery simply like this:
if($('#divId').length!==0){
Your Code Here
}
put the id you want to check in jquery is method.
var idcheck = $("selector").is("#id");
if(idcheck){ // if the selector contains particular id
// your code if particular Id is there
}
else{
// your code if particular Id is NOT there
}