Jump to content

ajax response not finding drop down...


mojito

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.