Robert
Robert

Reputation: 816

Have element change select form value

How can I link the actions of a click function to a select form with jquery.

For exmaple if an image 1 is clicked. How can I let the form know that image 1 is clicked by changing the 'selected' option in a form.

<a href="#" rel="green">Green</a>
<select>
 <option value="green">Green</option>
</select>

When "green" is clicked the "selected" option is added to the selection

I know i've seen it done before. I just cannot find it anywhere.

Thanks, Robert

Upvotes: 2

Views: 136

Answers (3)

jk.
jk.

Reputation: 14435

$('a').click(function() {
    $('select').val($(this).attr('rel'));
});

Demo: http://jsfiddle.net/MZ7nS/

Upvotes: 1

GregL
GregL

Reputation: 38121

Use the attribute selector to find the option with the corresponding value:

$('a').click(function() {
    var optionValue = $(this).attr('rel');
    $('select').find('option[value=' + optionValue + ']').prop('selected', true);
});

Upvotes: 0

Silvertiger
Silvertiger

Reputation: 1680

a bit if javascript magic and you're in business

<a href="#" rel="green" onClick="document.getElementById('colorselect').value = 'green';">Green</a>
<select id="colorselect">
    <option value="green">Green</option>
</select>

should work

Upvotes: 1

Related Questions