change selects value onchange of another select

i start learning jquery few days ago, and i like it very much. but now i have a problem, that can't solve alone.

i have two selects

<select id="select1"> <option value="1">1day</option> <option value="2">2day</option> <option value="3">3day</option> </select> <select id="select2"> <option value="1">1day</option> <option value="2">2day</option> <option value="3">3day</option> </select>

i need to set #select2 the same value with #select1, when #select1 changes

i've red some questions about select tag here, but i need to set "selected" attribute to that option, which have the same value. how can i do it?

Thanks

-------------Problems Reply------------

To set the value use .val() like this:

$(function() { //run on document.ready
$("#select1").change(function() { //this occurs when select 1 changes
$("#select2").val($(this).val());
});
});

You can see a working demo here

This would set <select id="select2"> to the same value as the other one has. .val(vale) selects the <option> that has the corresponding value in the <select>, also de-selecting the previous selection. You use .val() without parameters to get the currently selected <option>'s value.

Category:jquery Views:1 Time:2010-05-29
Tags: jquery

Related post

  • Does a form reset button fire a select elements onChange event? 2009-02-13

    I have a form with some select elements that have onChange events attached to them. I would like the event to fire even when someone clicks the form reset button. My question is: does resetting a form fire a select elements onChange event? Here is a

  • Problem with Select box Onchange event in html 2009-09-14

    <script> function options(){ alert("asdfasdf"); } </script> <select type="selectbox" name="crPaymentOption" id ="crPaymentOption"onchange="options()"/> <option selected="" value="--">--</option> <option value="Check"

  • Attaching onChange event to select list in Rails 2010-02-23

    I want to add a simple onChange event to a select list I have in Rails. How do I do this? This is the select box code: = f.select(:question_type_id, QuestionType.all.collect {|qt| [ qt.name, qt.id ]}, { :include_blank => "Please Select a Question

  • drupal Select cck onchange handler 2010-11-10

    i'm adding onchange handler to all cck fields by following manner. function bday_form_event_node_form_alter(&$form, &$form_state) { $form['title']['#attributes'] = array('onchange' => "titleval()"); $form['#after_build'][] = 'bday_form_eve

  • How to pass parameters on onChange of html select 2011-02-17

    I am a novice at JavaScript and jQuery. I want to show one combobox-A, which is an HTML <select> with its selected id and contents at the other place on onChange(). How can i pass the complete combobox with its select id and how can I pass othe

  • Converting selects with onchange to ul and jquery 2011-03-01

    I am trying to convert my select boxes into more stylish ul dropdowns but the selects are using onchange that calls a function with document.search.submit() in it I thought this was a good option for my purpose because I figured I could just hide the

  • multiple select element - onchange 2011-04-05

    I have a select element that allows for multiple selections. I'd like to display the selected values in another part of the page (in a div or something) as the user makes changes to what is selected. Is the only way of doing this to iterate over the

  • How to call a JavaScript function onchange of a select tag? 2011-05-28

    Below is my JavaScript: <script language="JavaScript"> function checkit() { var state=document.frm.getElementById(stateS) if (state="Please select a state") { document.frm.Month.disable = true; } else { document.frm.Month.enable = true; } }<

  • Load innerHTML inside DIV based on dropdown menu selection using onChange 2011-06-29

    Okay, so I've successfully completed my goal using buttons which was merely a test. My real goal is to dynamically change the innerHTML within a DIV, based on the option selected using the drop down menu. The catch however, is that it MUST be done us

  • Fill second select with onchange of first one on MVC3 without javascript 2011-08-12

    i have a project on MVC3 and Razor. I'm doing on my project everything without javascript first and then in the future when everything works without JS, then i will add the JS functionallity. The thing is that i have a razor page, that i fill from th

  • select and onChange in a Ruby on Rails form 2011-12-31

    I browsed all SO questions and answers about this topic but I'm still unable to make my scenario work. I want to trigger a click button action when a dropdown menu option is selected ; seems simple and should be very common with AJAX. Here are the re

  • How to find what caused onChange event for select box in IE - keyboard or mouse? 2012-02-07

    Is there some way to find out what caused the onChange event on select box in Internet Explorer (>= IE8) - keyboard or mouse? I have a code which doing something when user selecting a value, and this code works great in Firefox and Chrome but not

  • Select element onchange 2009-11-30

    How can i acces an select element if it has been selected? var select = document.getElementById("select"); if(select.selected == true) doesnt work any idea? --------------Solutions------------- document.getElementById("mySelect").value or var selObj

  • Change the text in the selected box of a select menu onChange event 2011-01-12

    I have a select menu with option items as <select> <option>--1</option> <option>--2</option> <option>--3</option> <option>--4</option> </select> When i select a item from the select menu, it

  • Redirect automatically when selecting an item from a select drop-down list 2009-02-24

    This page does it the way I want it to: http://www.web-source.net/javascript_redirect_box.htm But I want to find a better way using jQuery, can anyone point me to a good script that uses jQuery to accomplish this? --------------Solutions-------------

  • Add/Remove new Select w/ Options if Current Select w/ Options is selected 2011-03-08

    Ok I would like to be able to add another select box with the same options if the previous select box option is selected. If it's de-selected then remove the last select box w/ options. Here is what I'm trying, I can add/remove another select box wit

  • By selecting an option from the select menu how to force another select menu to automatically load a specific option? 2011-12-20

    By selecting an option from the select menu how to force another select menu to automatically load a specific option? What i mean is that i have this php code : <select id="video_cattegory_main" name="video_cattegory_main" style="display:inline; w

  • Disable Select Option dependent on another Select Box 2011-12-29

    I have three select boxes, that the user would choose in terms of priority. I have used sport for this example. At present I've set the css to hide the 2nd and 3rd priority selects until the first box has been completed and so on. The issue I have is

  • Show Hide select options based on previous selection dropdown 2012-02-09

    Thanks for reading my question... ;-) I'm building a Wordpress site that uses Custom Posts and Custom Fields to show a vehicle inventory. I would like the visitor to be able to filter the posts by Taxonomies... The plugin I use for drilling the avail

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.085 (s). 11 q(s)