I made a very simple button click event handler, I would like to have element be appended when button clicked, you can check my code
http://jsfiddle.net/j36ye/17/
$("#search_btn").click(function(){
if(($('#other').length) == 0) {
$("#wrapper").append("<p id='other'>I am here</p>");
}
return false
});
Or
var other_appended = false;
$("#search_btn").click(function(){
if(other_appended == false) {
$("#wrapper").append("<p id='other'>I am here</p>");
other_appended = true;
}
return false;
});
1) jsFiddle loads in MooTools by default, you need to include jQuery for this to work. There is not a reason in the world why that example wouldn't work. (Assuming that the $
is actually mapped to the jQuery
object, that is.)
2) You can check the nextSibling
of a DOMElement
, or use the next()
jQuery method, like so:
if(!$('#something').next().length) {
//no next sibling.
}
You are using mootools and not jQuery.
To check if your element exists
if($('#other').length > 0)
So if you do not want to append the element twice:
$("#search_btn").click(function() {
if($('#other').length == 0) {
$("#wrapper").append("<p id='other'>I am here</p>");
}
});
Or, you can use the .one(function)
[doc]:
$("#search_btn").one('click', function() {
$("#wrapper").append("<p id='other'>I am here</p>");
});
How to check if an element exists:
if($("p#other").length > 0) {
// a p element with id "other" exists
}
else {
// a p element with id "other" does not exist
}
Your fiddle is using the moo tools framework. Change it to use the jquery framework on the left and it works. See http://jsfiddle.net/KxGsj/
check if element is already appended:
alert($(this).parent().length?'appended':'not appended');