JQuery event listener not triggered when added dynically [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



JQuery event listener not triggered when added dynically [duplicate]



This question already has an answer here:



Currently trying to dynamically toggle content within an HTML document using JQuery based upon a user click.



I have an HTML document and I have the following JQuery code, which dynamically toggles the contents of the div based upon a users click.:


div




$(function()
$(".uen").on("click", uenSearch);
);

function backButton ()
$(".search-form, .content-wrapper.back").toggle();
$(".uen > h3, .uen > p").toggle();
$(".uen").on("click", uenSearch);


function uenSearch ()
$(".uen > h3, .uen > p").toggle();
$(".search-form, .content-wrapper.back").toggle();
$(".uen").off();
$("#back").on("click", backButton);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Search boxes by UEN & Equipment type -->
<div class="search-box uen">
<h3>Search by</h3>
<p>UEN</p>
<form action="#" method="post" class="search-form">
<input type="text" placeholder="Enter UEN Number" name="uen_number"
required>
<button type="submit">SEARCH</button>
</form>
<div class="content-wrapper back">
<img src="images/back_arrow-white.svg" alt="back-arrow" width="25">
<p id="back">Back</p>
</div>
</div>



However, once the user has clicked on the div the first time and the content is changed, if the user tries to click on the "#back" paragraph the event listener is not triggered to toggle the div's content back to it's original.



Any help in solving this would be much appreciated.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





My guess is you are not preventing form submit and the form submittal reloads the page
– charlietfl
Aug 6 at 14:25




1 Answer
1


$(function()
$(".uen").on("click", uenSearch);
);



change it to


$(document).on("click", ".uen", uenSearch);



This post talks about event listners in dynamic elements more:
Event binding on dynamically created elements?





it seems you have a typo in the last few character
– Yong Quan
Aug 6 at 14:24





That will invoke uenSearch() immediately ... not when the event occurs
– charlietfl
Aug 6 at 14:26



uenSearch()





you're wrong. It will invoke it on the click of .uen
– Budyn
Aug 6 at 14:29





Replace the document variable with a selector of the closest static element if you want to use 'live" functionality. Using document for this is bad practice.
– Mark Baijens
Aug 6 at 14:29






$(".uen").on("click", "#back", backButton); is probably what you need.
– Mark Baijens
Aug 6 at 14:34


$(".uen").on("click", "#back", backButton);

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard