Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8204653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:57:49+00:00 2026-06-07T07:57:49+00:00

I have two select-boxes, each of them is in span element with specific id.

  • 0

I have two select-boxes, each of them is in span element with specific id. The first select-box is country, the other one is city list. When I select a country the innerHtml of city select-box container span is replaced by response of ajax function everything is good. But I want to fire a function by useing onchange attribute of selectbox if at the begining of page loading addeventListener function works fine but affter replacement of innerHtml of span it does not work and after replacement the id of city selecbox is same es before replacement. I am just using.

document.getElementById('to_city').addEventListener('change',doSomething,false);

this code is initialized by onload of window element.

It works without ajax replacement but dont after.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:57:51+00:00Added an answer on June 7, 2026 at 7:57 am

    When you replace contents with AJAX you need to bind event again. so its good idea to bind event again after replacing content.

    There is a way to avoid it using event bubbling.

    JQuery 1.4+ supports onchange event propagation so you can use jquery delegate function to achieve same. http://api.jquery.com/delegate/

    with Jquery only one line do the work.

    $("#container").delegate("#to_city", "change", doSomething); // Workd in all browsers
    

    I ignored IE in below example as you are using addEventListener which is not supported by IE.
    Without Jquery
    Working example (Not working in IE) : http://codebins.com/bin/4ldqpb1/2

    document.getElementById("container").addEventListener( 'change', function(e) {
        var targ=e.target;
        if (targ.id == "to_city") { //you just want to capture to_city event
            //DO STUFF FOR SELECT
            //DO STUFF
            doSomething();
            alert("id: " + targ.id + "\nvalue: " + targ.value);
        }
    
    }, false)
    

    Explanation:

    When you bind any events they are bound to that particular element not the selector or ID. so when you replace the content and new replace element with the new element with the same ID, new element doesnt have any event attached. so you need to attache event again. so if you attach event after AJAX content replace doSomething will work. but that is not a very good solution.

    We are using event bubbling concepts. many events are bubbled to the top of the document. (IE doesn’t bubble change event)
    So we write handler on the container and listen for the event. each event has a target or srcElement which says which dom element firing this event. if the dom element is what we are looking for execute function. in this can we are looking for element with ID to_city that’s why if condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two select boxes, the first select box has two options: First set
I have two select boxes (one for Car Types and the other for Car
I have a form with two select boxes: country and city, city depending on
I have a form with two text boxes, one select drop down and one
I have two combo boxes, one for 'order by' and the other 'direction' (ascending,
I have two pages. One with the select boxes on and the send button.
I have I have two pages. One with the select boxes on and the
I have two HTML select boxes. The first is called #1 and the second
I have two select boxes, the second of which is populated once the first
I have two multi-select list boxes; one with months and one with years such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.