Dika Dika - 1 year ago 78
Javascript Question

Disable a href after click once and add new element at the same time?

Is it posible to add new element with javascript by using href and disable href at the same time?

So href only clicked once, and after that the new element will appear.
I wrote this code, but still no luck


$(document).ready(function() {
$("#add_app").click(function() {
$("#box").append("<p>jQuery is Amazing...</p>");


<a href="#" id="add_app" ><strong>Summon new element and disable me</strong></a>

The only does the job is in JS line

$("#box").append("<p>jQuery is Amazing...</p>");

Need help..


-Using improved code by PacMan, Linus Aronsson, and guest271314 and it works like a charm.
So basically we use .one() event handler to solve the problem.
Thanks for the feedback guys

Answer Source

Use this jQuery:

$(document).ready(function() {
    $("#add_app").one("click", function() {
    $("#box").append("<p>jQuery is Amazing...</p>"); 

I'm using the .one() event handler to indicate that you only want the click event to take place once. The rest of the code was more or less correct with a few small changes.

Here's a fiddle: https://jsfiddle.net/0mobshpr/1/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download