tags:

views:

156

answers:

5

Hi, I have the following HTMl:

<input id="brandID" name="brandID" type="hidden" value="" />

<select id="selectList" class="textfield70pc" name="selectList">
    <option selected="" value="221">A</option>
    <option value="4673">B</option>
</select>

Upon changing the value of a select box, I need to populate this input field. I've tried something like this, but it's not working.

      jQuery('#selectList').change(function(){
        jQuery('#brandID').attr('value', '123123');
      });

Suggestions anyone?

Update
This works though:

document.getElementById('brandID').value = '123'

But I would like to use jQuery.

A: 
jQuery('#selectList').live('change', function(){
  jQuery('#brandID').value = '123123';
});

And remember that IE won't fire the onchange event until the select loses focus.

Scott Saunders
A: 

Does this work?

jQuery('#selectList').bind('change', function(){
  jQuery('#brandID').val('123123');
});

EDIT:

From the jquery Live() documentation page:

Added in jQuery 1.3: Binds a handler to an event (like click) for all current - and future - matched element. Can also bind custom events. Possible event values: click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup Currently not supported: blur, focus, mouseenter, mouseleave, change, submit

JamesEggers
Nope. That doesn't work either :(
Steven
@Steven can you provide your markup for your selectList? The issue may be there and not associated with the hidden input field.
JamesEggers
+5  A: 

Change is not supported in jQuery live

Possible event values: click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup

Currently not supported: blur, focus, mouseenter, mouseleave, change, submit

If you want to use live, use live query


EDIT: Your code above should work as it is, so maybe your selectList is formatted improperly?

This basic HTML made the above function work for me:

<select id="selectList">
 <option>1</option>
 <option>2</option>
</select>

Hmmm maybe you're just not seeing it update the HTML, but it does update the DOM (are you using firebug?) I added an alert to show the value and it showed the correct value

$(document).ready(function(){
 jQuery('#selectList').change(function(){
  jQuery('#brandID').attr('value', '123123');
  alert($('#brandID').val());
 });
})
fudgey
Or just bind the change event to a container element that doesn't ever get replaced, then check that the changed element is your #selectList.
Justin Stayton
Ah, I forgot. Ok, so I'll just use $('#selectList').change instead. But still not able to add the value
Steven
ok, that works. But the html is not updated - from what I can see using Firebug. Need to bind the change somehow.
Steven
Firebug doesn't seem to update the HTML, but if you start with Firebug minimized, change the selector, get the alert, then open firebug you will see the HTML is updated... The value is there, you shouldn't have to worry about it not showing up in Firebug.
fudgey
Thanks Fudgey. It's all working fine "all" browsers (IF, FF, Opera) now. It's heavy doing brainwork when you are still sick :op
Steven
A: 

just at first glace:

$(document).ready(function()
{
    $('#selectList').change(function() { $('#brandID').val('123123'); });
});

my assumptions: selectList is a valid ID somewhere.

IF this is an actual select list (implied by the name only), do you care what is selected?

$(document).ready(function()
{
    $('#selectList').change(function() { 
        mychoice = $('#selectList option:selected').text();
        $('#brandID').val(mychoice); 
    });
});
Mark Schultheiss
A: 

After reading all these good responses that haven't worked for you I have to ask the basic question -- are you running this Javascript within $(document).ready() or in a <script> tag after the <select> element? It has to be one or the other so that the DOM tree to contain the element before you set its properties.

Kai