0

I have the following code that loads element from a remote page (under same domain) into the current page. The element #Beijing contains a dropdown menu, and I wish to add some value into the menu in select syntax after it is loaded. The value I would like to add is:

onChange="this.form['CAT_Custom_221342'].value=this[this.selectedIndex].value"

and here is the code I've got, please help me to edit it furthur, thank you.

function StateBeijingNext() { 
$('#SuburbSelect').load('/Country/CN/_CN_suburbs.html #Beijing');   
}
1
  • Without seeing your html we can't be sure what exactly you require. Is #Beijing actually the <select> element? Commented Aug 30, 2012 at 7:38

2 Answers 2

1

There is no need to insert javascript event handlers into your html; this is difficult to follow logically when debugging, and is simply poor style, since you don't have a clean separation of content and functionality. Since you are using jQuery, leverage its event binding capabilities:

$('#SuburbSelect').on('change', '#Beijing', function() {
    this.form['CAT_Custom_221342'].value = this[this.selectedIndex].value;
});

You can bind this event handler as soon as the DOM is ready, and when your new content is inserted the change handler will work as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks nbrooks, I think I need your very first code since I need to keep this part for a reason, could you please please sent me the first version of your answer? Thanks
@wanghaipeng Sorry I'm not sure what you mean, I don't think I changed anything...what exactly do you need to keep the same? Oh if you mean the additional function wrapper on the outside, that's just a dom ready function which tells the browser not to run this until the dom is ready. If you already have one, just drop my inner code directly into yours.
1

.load() accepts a callback function that allows you access to the elements you're loading after they're been loaded, see below:

function StateBeijingNext() { 
    $('#SuburbSelect').load('/Country/CN/_CN_suburbs.html #Beijing', function(){
      //the element has been successfully loaded into the DOM.
      //peform your custom logic against the #Beijing element here.
    });   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.