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
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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.