Jump to content

Do event listeners work with ajax?


longtone

Recommended Posts

I'm pretty new to unobtrusive js, and I don't know if this should work or not

 

This is a simplified version:

 

<div class="videoindexbox" id="sbb">
<form class="short_form" id="searchbigbox" action="/" method="get"  >
    <input class="submit_button" type="submit" id="searchbigbox_submit_btn" name="btn_videos" value="Go" />
</form>
</div>

 

And I've set an event listener:

function searchBigBoxListener() {


if(!document.getElementById || !document.getElementById('searchbigbox_submit_btn') || !document.createElement) return;

   var btn = document.getElementById('searchbigbox_submit_btn');
		btn.onclick = function(){ searchBigBox(); hide(document.getElementById('hidden_search')); return false};

}

 

When I load the page, it works fine.

 

But if I then repopulate #sbb using ajax, the event listener doesn't work

 

I am using innerHTML for this, so maybe that's the problem?

Link to comment
https://forums.phpfreaks.com/topic/193058-do-event-listeners-work-with-ajax/
Share on other sites

The event listener is bound to an individual element. When you repopulate the div, the old element is removed and new ones are added. They're entirely new, even if they share the same tags, names, ids.  At that stage, you will need to add the event listener to the new submit button.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.