mojito Posted July 13, 2006 Share Posted July 13, 2006 I have the following block of code which cant find the dropdown im trying to populate.[code] var sel = document.places.sel2; alert("sel") //clear old content while (sel.options.length) { sel.remove(0); } //split the incoming string to create array arrOpt which contians the pairs of content var arrOpt = response.split("|"); for(var i = 0; i< arrOpt.length-1;i++){ var arrVal = arrOpt[i].split("~"); sel.options[sel.options.length] = new Option(arrVal [0],arrVal[1]); }this is the dropdown<form id="places" name="form1" method="post" action=""> <select name="sel1" onchange="javascript:sendRequest('foo')"> <option>unnamed1</option> <option>unnamed2</option> </select> <select name="sel2" onchange="javascript:alert(this);"> <option>unnamed1</option> <option>unnamed2</option> </select></form>[/code]thats where the problem lies especially with the line var sel = document.places.sel2;sel should be a ref to the dropdown to save on typing etc.. but it doesnt find my dropdown. I get confused between name and id as identifiers.thanks for any helpmojito Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/ Share on other sites More sharing options...
nogray Posted July 13, 2006 Share Posted July 13, 2006 First make sure the page load before you try to access any objects, but your script in a function and call in when the page load.Also, it's always better to use document.getElementById Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-57403 Share on other sites More sharing options...
mojito Posted July 13, 2006 Author Share Posted July 13, 2006 ThanksI tried this [code]var sel=document.getElementById("sel2");alert(sel);[/code]returns or rather alerts "null"so it still doesnt work.hmmmmm. Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-57411 Share on other sites More sharing options...
nogray Posted July 13, 2006 Share Posted July 13, 2006 when you use document.getElementById, the object has to have an id property[code]<select name="sel2" id="sel2".........[/code]I tried your original code, and I found out you have an id for the form, but not a namejust add the name property to the form and it should work[code]<form id="places" name="places"............[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-57417 Share on other sites More sharing options...
mojito Posted July 13, 2006 Author Share Posted July 13, 2006 now in my js console i get document.places has no properties[code]var sel= document.places.getElementById("sel2");[/code]still not havin it! Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-57449 Share on other sites More sharing options...
nogray Posted July 13, 2006 Share Posted July 13, 2006 you miss understood my commentsIf you want to use document.getElementById("sel2") the select have to have an idyou cannot have document.places.getElementByIdIf you use your same code above (the same script you posted) all you need to do is add the name to the form[code]<form id="places" name="places"............[/code]and keep the sel as is[code]var sel = document.places.sel2;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-57500 Share on other sites More sharing options...
mojito Posted July 14, 2006 Author Share Posted July 14, 2006 Hi no greyNo I got you but I tried both ways as well anyway. Nothing is working, I need to return to a simple example and get that working in ff, then see why it isnt working for the ajax, though at this stage it should be very simple.I should say that this javascript is running inside a CMS joomla to be exact. But I dont think this should affect it.thanks for the help, what might I do to get a simple version working. Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-57902 Share on other sites More sharing options...
nogray Posted July 14, 2006 Share Posted July 14, 2006 Ok, I am attaching a version without the ajax stuff, you'll notice I have names and ids for everything. This should work in any browser[code]<html><head><title></title><script language="javascript">function which_sel(){ // you can do this var sel = document.places.sel2; alert(sel); // or this var sel2 = document.getElementById('sel2'); alert(sel2);}</script></head><body onload="which_sel();"><form id="places" name="places" name="form1" method="post" action=""> <select name="sel1" id="sel1" onchange="javascript:sendRequest('foo')"> <option>unnamed1</option> <option>unnamed2</option> </select> <select name="sel2" id="sel2" onchange="javascript:alert(this);"> <option>unnamed1</option> <option>unnamed2</option> </select></form></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-58003 Share on other sites More sharing options...
mojito Posted July 14, 2006 Author Share Posted July 14, 2006 im assuming its a typo but you have 2x name attributes on the form.I will try this code as standalone in my ap. But it doesnt work with the ajax.thanks Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-58048 Share on other sites More sharing options...
mojito Posted July 14, 2006 Author Share Posted July 14, 2006 Thanks for your support, i realise it is something to do with the CMS (joomla) as if the page isnt being sucked through that it is fine and works, but I need to go to that forum to check there now.thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/14488-ajax-response-not-finding-drop-down/#findComment-58064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.