Jump to content

Archived

This topic is now archived and is closed to further replies.

mojito

ajax response not finding drop down...

Recommended Posts

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 help

mojito

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thanks

I tried this [code]var sel=document.getElementById("sel2");
alert(sel);

[/code]
returns or rather alerts "null"

so it still doesnt work.
hmmmmm.

Share this post


Link to post
Share on other sites
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 name
just add the name property to the form and it should work
[code]
<form id="places" name="places"............
[/code]

Share this post


Link to post
Share on other sites
now in my js console  i get document.places has no properties
[code]var sel= document.places.getElementById("sel2");[/code]

still not havin it!

Share this post


Link to post
Share on other sites
you miss understood my comments

If you want to use document.getElementById("sel2") the select have to have an id
you cannot have document.places.getElementById

If 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]

Share this post


Link to post
Share on other sites
Hi no grey

No 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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.