Jquery: Append Div Only Once 'on Click'
I am trying to append a section to a div on the click of a button. I want the section to be appended only on the first click, the code that I use below appends a section every tim
Solution 1:
You could use one()
, which fires only once
$("#nextractorapply").one('click', function () {
// executes only once
$("#main").append('<section id="nextractor" class="five"> </section>');
});
$("#nextractorapply").on('click', function () {
// executes every time// do other stuff
});
Solution 2:
Use a conditional to determine if it's there.
$("#nextractorapply").click(function () {
if($('#nextractor').length < 0){
$("#main").append('<section id="nextractor" class="five"> </section>');
}
});
Solution 3:
You can use some kind of a condition that prevents its appending multiple times.
var counter=0;
$("#nextractorapply").click(function () {
if(counter<=0){
$("#main").append('<section id="nextractor" class="five"> </section>');
counter++;
}
//YOUR OTHER STUFF THAT YOU NEED TO EXECUTE ON EVERY CLICK
});
Solution 4:
You could use .unbind()
$("#nextractorapply").unbind().click(function () {
$("#main").append('<section id="nextractor" class="five"> </section>');
});
Solution 5:
You could set a variable and fire event if variable is set as true or false.
var _clicked = false;
$('.element').click(function(){
if(!_clicked){
_clicked = true;
console.log('event fired');
}
})
Post a Comment for "Jquery: Append Div Only Once 'on Click'"