I’m new to javascript and need help to figure this one.
I have three menus where you select and option and for each menu you select it displays a row of data. In each row (i.e. 3 row for 3 selections) another select menu is available for you to pick a provider for that product. I created another select that you select a default provider and onchange the other menus in the rows change. The problem I have is that they all have the same id because when you pick an option a function gets called. I tried to loop through all select and from those selects with id=x change selected option but only works for 1st select found with that id
echo" function pickprovider($name, $idname){ \n";
echo" <select name='$name' id='idname'> \n";
echo" <option>1stprovider </option> \n";
echo" <option>2ndprovider </option>\n";
echo " </select>\n";
echo" }\n";
echo" function defaultprovider($name, $idname) {\n";
echo" <select name='$name' id='idname' onchange='changeDflt(this);'> \n";
echo " <option>1stprovider </option> \n";
echo " <option>2ndprovider </option> \n";
echo "</select>\n";
echo" }\n";
echo "<script type='text/javascript'>\n";
echo "function ChangeDflt(list) { \n";
echo "var x = list.options[list.selectedIndex].text;\n";
echo "var allSelects = document.getElementsByTagName('select');\n";
echo "for (var i = 0; i < allSelects.length; i++) { \n";
....
change selected options for selects
echo "}\n";
echo "}\n";
echo "</script<\n";
I do not know how to change the function to make a unique id everytime the function is called. ….Can I use “div” tag when I get all the select elements in page?
I’m not really sure what you’re trying to accomplish with the
selectelements but if you structure your PHP to emit HTML and JavaScript differently, it gets both easier to read and easier to write both your HTML and your JavaScript.That said, syntactical constructs like:
are completely invalid JavaScript code and will not run. You may want to review a tutorial on JavaScript. I recommend this one: http://www.hunlock.com/blogs/Essential_Javascript_–_A_Javascript_Tutorial. It starts with the basics and explains JavaScript far better than I can.