... yourfunction()..." />

Javascript how to run the same function onLoad and onChange?

This could happen when you receive postback from your remote server and your response doesn't have script type="javascript">... yourfunction()...

Up vote 1 down vote favorite share g+ share fb share tw.

I have a form that has many select menus, most of them are Yes/No and depending on the selected option, I display/hide some advanced options. One of the select menus is the following: "cbi_available_id", :class=>"cbi_available_class", :onChange=>"showHideOptions('cbi_available_id','cbi_options_id')", :onLoad=>"showHideOptions('cbi_available_id','cbi_options_id')"} %> When I change from 'Yes' to 'No' or the opposite, showHideOptions javascript functions is called properly, but I can't have that function to be called when I reload the form. Anyone can tell me what am I dong wrong?

Thanks UPDATE function showHideOptions(selectorId,optionsId) { if (document. GetElementById) { var selector = document. GetElementById(selectorId); var options = document.

GetElementById(optionsId); if (selector. Value == 'Yes') { options.style. Display = 'block'; return false; } else { options.style.

Display = 'none'; return false; } } window. OnLoad = showHideOptions('cbi_available_id','cbi_options_id'); javascript forms select onload onchange link|improve this question edited Mar 28 at 6:43 asked Mar 28 at 6:08rh4games727 74% accept rate.

I don't understand. Onload gets called when the form is (re)loaded. What were you expecting?

– Ken Thompson Mar 28 at 6:14 in my case, :CBIAvailable = 'No', so I expect showHideOptions function to execute when I reload the form and it should display the relevant options for 'No', but in fact the form reloads with the options for 'Yes'. When I change the select menu to 'No', it works fine, and when I change it back to 'No' it works as well. The problem is it does not display the right options onload – rh4games Mar 28 at 6:23.

This could happen when you receive postback from your remote server and your response doesn't have . Each time you get new response you should send script and exacute it or append to your html element approciate event handler. Another solution is to use jQuery and use .live() event.

This event attach dynamically behaviour to your html. I strongly recommend you to use jQuery with live because this library is one of most used libraries in production environment. Edit.

Thanks r.piesnikowski. I do have Can you please help with a sample jQuery script that uses /live()? – rh4games Mar 28 at 6:29.

Function yourFunction(){ //get that select element and evaluate value //do you change stuff here } window. Onload = yourFunction(); //this gets fired on load //"select" is your element, //fetched by methods like document.getElementById(); select. Onchange = yourFunction; //this gets fired on change //you can also use attachEvent (IE) or addEventListener (Others) here's a working demo: YES NO?

Function getOption() { alert('foo'); } var select = document. GetElementById('testSelect'); select. Onchange = getOption; window.

Onload = getOption().

Thanks Joseph. I updated my question above with the changes you proposed, but it still does not work fine when I reload the form. It does work fine when I change the selected option from 'Yes' to 'No' and the opposite.

– rh4games Mar 28 at 6:45 updated my answer – Joseph Mar 28 at 6:48 Still does not work. In fact, with the original syntax, the function does fire onload (I tested by adding a document. Wrtie inside the function) but it does not do the rest like when I select 'Yes' or 'No' – rh4games Mar 28 at 6:55 updated, and now with a demo an corrections :D – Joseph Mar 28 at 7:04 1 I finally figured out how to solve my problem.

I had to put the window. Onload line in a section at the bottom of the file, after the form. I guess that way window.

Onload is executed after the form loads and therefore works fine. When window. Onload is in a section at the top of the file, document.

GetElementById(selectorId) points to null and therefore has no value to test against. – rh4games Mar 287 at 20:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions