Topic: setting a select box via javascript

I've got a select box that I'd like to change via javascript.  If it were a textfield I could use

window.parent.document.getElementById('client_name').value = '<%= @client.client_name %>';

Unfortunately the above code doesn't work for a ddl though.  Is there an equivalent?

TIA..

Re: setting a select box via javascript

Use the force... wait, wrong forums.

Use innerHTML and set the individual options.

Also, you COULD use document.getElementById('element_id')... OR!

You could use $('element_id') if you're using the prototype javascript library.

[code="innerHTML Works Wonders"]
$('element_id').innerHTML = "<option value='1'>Option 1</option>"
[/code]

Of course you can specify multiple elements. I'm just lazy.

Re: setting a select box via javascript

Ended up using:

window.parent.document.getElementById('client_name').value = "<%=client.id %>";

I probably should learn more prototype though..

Re: setting a select box via javascript

I dont see how that would work. If you're having a drop down list, and you're setting the value attribute on it, well... there just isn't a value attribute! Are you sure this is working because I think that it wouldn't.

I could be wrong, as last time I checked I was Human.

Re: setting a select box via javascript

Well, I'm using dummy data but it does appear to be working.  But you do look a bit like the terminator in your current avatar pic so I may check again.. smile

Re: setting a select box via javascript

Radar wrote:

Use innerHTML and set the individual options.

[code="innerHTML Works Wonders"]
$('element_id').innerHTML = "<option value='1'>Option 1</option>"
[/code]

From my experience, this does not work in IE until version 6...I ended up with putting

    i=0
    @var.each {|a|
      page << "$('id').options[#{i}] = new Option('#{a[0]}','#{a[1]}')"
      i = i+1
    }

in my RJS to update multiple select options.

Which browsers did you test with the innerHTML solution?

Re: setting a select box via javascript

For a moment there I did that thing people do, you know, put one hand on the side of the face and slowly drag it downwards whilst making a groaning noise.

Then I re-read what you had written:

This does not work in IE until version 6

Thankfully there was this new operating system released back in 2001 (October 25th, for those of you playing along at home) called Windows XP. This operating system came standard with Internet Explorer Version 6.0 (six-point-oh). Unfortunately, the majority of people were still using Windows ME, or worse, Windows 98, so the "standard" back in 2001 was what came with those operating systems which was Internet Explorer Version 5.5 (five-point-five).

Fortunately the wonderful people at Microsoft decided that on 11th July 2006 that Windows ME and Windows 98 were to be no longer supported! Fantastic news for Microsoft, of course, this meant that more people had to "upgrade" to Windows XP, therefore giving Microsoft more money in which they were to put into a burning pit of lava they call "Vista".

My point is this (and honestly, I'm surprised you're still reading), Windows ME and Windows 98 were basically deprecated in 2006. This means anyone using those had to upgrade, meaning they are now *most likely* running Internet Explorer 6 or Mozilla or Opera or Safari or some other browser, and not Internet Explorer 5.5. If we were to support all the older versions of all the software ever created we would still be coding in Fortran and COBOL, or worse, Perl.

Thankfully, old technology is eventually deprecated meaning that, as web developers, we can say "that is no longer supported, please upgrade" and get away with it. Anyone still running Windows 98 and Windows ME deserves to be shot anyway.

That is all.

Re: setting a select box via javascript

Well sorry that I have written mistakenly until version 6. It should have been version <= 6

But anyway...you're really a cool guy.

Re: setting a select box via javascript

So it's not really supported in IE6? I was just being a pompous ass and thought that it was. I'll investigate later in the week and get back to you.

Re: setting a select box via javascript

You can't just add options like this in IE, Its not very nice I know and made me swear for a while in IE you have to do

var foo = $('SelectBoxID');
// if you want to clear any current options
foo.options.length = 0
for ( x = 0; x <= 5; x++ ) {
  foo.option[x] = new Option("Dachshund", "dachs")
}

This may help : http://www.devguru.com/Technologies/ecm … ption.html

Re: setting a select box via javascript

IE needs to be put out of its misery.

Where's my shotgun?

Re: setting a select box via javascript

pimpmaster wrote:

IE needs to be put out of its misery.

Where's my shotgun?

Oh sorry, I was using it to massacre ActiveRecord.

Re: setting a select box via javascript

Just to add to the thread (since someone mentioned it), you can't set the innerHTML of a select list in IE, it will fail.

http://webbugtrack.blogspot.com/2007/08 … lists.html