Forum Moderators: open

Message Too Old, No Replies

Chained select doesnt seem to trigger onChange event

         

DrunkEric

4:42 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



index

<script language = "javascript">
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function getMenu(divID)
{
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", document.getElementById("parent").value);

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}

XMLHttpRequestObject.send(null);
}
}
</script>

</head>
<body>
<form name="routercfg">
<select id="parent" onChange="getMenu('models')">
<?php include "./php/brands.php";?>

</select>

<div id="models">
<select id="model" onChange="getMenu('config')">
<option>Select Router Model</option>
</select>
</div>

<div id="config">
<select id="config">
<option>Select Router Config</option>
</select>
</div>


</form>
</body>
</html>

example option line from parent div


<option value="./php/cisco.php">Cisco</option>

example option line from models div


<option value="./php/config.php?model=1721C">

The first select "parent" the onChange does trigger and load the models div correctly however the onChange in "models" does not seem to trigger when making a change. I have watched it in firebug and it confirms this. Since the first onChange event works properly it would seem the javascript portion of things is working properly, but I cant seem to figure out why it wont trigger the second time.

Anyone have any ideas?

penders

5:09 pm on Jan 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The onChange event will only fire if the user actually changes it, unfortunately it will not fire as the result of manipulating elements via JavaScript. Is this what you are experiencing?

DrunkEric

5:18 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



If I select any option from the 2nd drop down the onChange event doesnt trigger. The second list contains about 15 items. If I select any of them the 3rd box does not change, just keeps the initial option

penders

5:26 pm on Jan 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<div id="config">
<select id="config">

Ah, you seem to have two IDs the same.

Are you testing the 2nd dropdown immediately after the page has loaded? Or after the contents has been written with JavaScript? Just wondering whether may be only the OPTION elements should be written with JS, not the entire SELECT, since you have an event assigned to the SELECT ...?

DrunkEric

6:30 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Man I was hoping the div id was the answer on this one, but didnt seem to make a difference.

Are you testing the 2nd dropdown immediately after the page has loaded? Or after the contents has been written with JavaScript? Just wondering whether may be only the OPTION elements should be written with JS, not the entire SELECT, since you have an event assigned to the SELECT ...?

After the the contents have been written. Not sure if I understand the second part of the question so I will answer with what I think you are asking. The php pages that are called from the onChange only write the options. This is the same way I am doing it with the first one so I dont think that is part of the issue. If I misunderstood what you were asking in the second part just let me know.

phranque

1:52 am on Jan 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], drunkeric!

according to w3c's description of intrinsic events [w3.org]:

The onchange event occurs when a control loses the input focus and its value has been modified since gaining focus.

i haven't tried this myself, but perhaps you could use the document.formname.fieldname.focus() javascript method to set focus on the select field before changing it and then move the focus to another field afterwards to trigger the onChange event.

mehh

10:30 am on Jan 6, 2008 (gmt 0)

10+ Year Member



Man, I can't beleve you guys overlooked this

XMLHttpRequestObject.open("GET", [b][i]document.getElementById("parent").value[/i][/b]);

Try this

<html>
<head>
<script [b]type="text/javascript"[/b]>
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function getMenu(divID,[b]srcEl[/b]) {
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", [b]srcEl.value[/b]);
XMLHttpRequestObject.onreadystatechange = function(){
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}
XMLHttpRequestObject.send(null);
}
}
</script>
</head>
<body>
<form name="routercfg">
<select id="parent" onChange="getMenu('models',[b]this[/b])">
<?php include "./php/brands.php";?>
</select>
<div id="models">
<select id="model" onChange="getMenu('config',[b]this[/b])">
<option>Select Router Model</option>
</select>
</div>
<div id="[b]configs[/b]">
<select id="config">
<option>Select Router Config</option>
</select>
</div>
</form>
</body>
</html>
Changes in bold

phranque

11:45 am on Jan 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Man, I can't beleve you guys overlooked this

man, i don't do code dumps.

penders

12:39 pm on Jan 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



DrunkEric: After the the contents have been written. Not sure if I understand the second part of the question so I will answer with what I think you are asking. The php pages that are called from the onChange only write the options. This is the same way I am doing it with the first one so I dont think that is part of the issue. If I misunderstood what you were asking in the second part just let me know.

Ok, yes I agree, only the OPTION elements should be written. However, on glancing at your code you seem to be passing the divID and then changing the innerHTML of this obj, but this includes the SELECT as well?

And this also seems to be an inconsistency in mehh's code above. 'models' refers to a DIV, whereas 'config' refers to a SELECT? Both are passed to the getMenu() function.

DrunkEric

1:30 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



mehh: Tried your example code (with and without the config/configs id pass from the second onChange) and no luck with that. Firebug doesnt show the select throwing the trigger to the function) However with your help and the note below I now have it working the way I was tr

penders: You got me looking in the right direction. It was kind of a stupid mistake that I was overlooking. The reason it wouldnt trigger the event is because it didnt know about it. When it switched out the select, the new one that was loaded from the outside php script did not have the onChange event in the select. So since it wasnt in the code it didnt know to trigger it

Thanks everyone for your help.