views:

45

answers:

1
$('.reset').click(function () {
    $('select[name=one]').val(opt2);
});​

JSFiddle here.

What should I change?

Thanks!

+4  A: 

Put opt2 in quotes for val(). It's coming up as an error because it is undefined.


$('.reset').click(function () {
    $('select[name=one]').val('opt2');
});​

You asked for some other methods, but I only know of one other. This method doesn't require jQuery to function (note that the [0] grabs the normal Object rather than an jQuery wrapped Object).

//Without jQuery... pretty sure I'm correct with the second method, but I'm rusty on that one.
document.getElementById('formID').one.selectedIndex = 0; // Sets the select back to the first option.
document.formName.one.selectedIndex = 0;

//With jQuery
$('select[name=one]')[0].selectedIndex = 0;
Jeff Rupert
ahk, thanks, but are there any other alternate methods to select an option? thanks
Nimbuz
Strange, but enclosing in quotes or doublequotes still doesn't work. I added an alert at the end of the click event and found that the options are selected as required but return to what they were after the click event. weird!
Nimbuz
Enclosing with quotes does work: http://jsfiddle.net/q3ZzH/ Click reset and the selected option is "opt2".
Gert G
@Nimbuz: Re: `"enclosing in quotes or doublequotes still doesn't work"` Then there is an issue in code that you are not showing us. Link to the page or post code that duplicates the new problem.
Brock Adams
@Nimbuz: I don't have that problem when running the jsFiddle demo. Sounds like Brock is right if you're doing something else in your code that we don't see. I'm running FF 3.6.8 on Snow Leopard. What about you?
Jeff Rupert