How do I make it so the text is only added once? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How do I make it so the text is only added once? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

$("#paragraph").on("click", function(event){$("#paragraph").append(' Whoah! I didnt even know I could do that! I am a RAINBOW!')
event.preventDefault();})

I tried adding event.preventDefault() simply because I know that has to do with actions being permanent in some way or another. I honestly have no idea what it does. My problem is I want the text to only be added once no matter how many times the paragraph is clicked, currently, it keeps adding the text so that if you keep clicking it the text repeats.

Answer

JQuery has a .one(..) event that does exactly what you want. It attaches an event hander that fires only once.

$("#paragraph").one("click", function(event){$("#paragraph").append(' Whoah! I didnt even know I could do that! I am a RAINBOW!')});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="paragraph">I am the paragraph</p>
We are here to answer your question about How do I make it so the text is only added once? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji