user1591668
user1591668

Reputation: 2883

jquery how to get Text from dropdownlist

I got 2 pages.. but I am having difficulty getting the Text value from a dropdownlist and inserting it into a textbox for instance this is Page 1

page2 is where the newlist is

<select name="service" id="service" style="background-color:red;">
<option>automotive</option>
<option>mechanic</option>
</select>

So again the gets the dropdownlist from Page2 but for some reason I can't transfer that value into the textbox any help would be great..

Upvotes: 1

Views: 4010

Answers (3)

Irvin Dominin
Irvin Dominin

Reputation: 30993

Try using val instead of text.

val

The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.

text

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

Code:

 $("#pick").change(function () {
     if ($(this).val() == "Financial") {
        $("#keyword").val($("#service").val());
     }
 });

Demo: http://jsfiddle.net/IrvinDominin/TKeE6/

Upvotes: 1

Niks
Niks

Reputation: 4832

As far as I can see, the last part of code should be executed in the complete event.

  $("#newlist").load('/Listings/service #service',function(){
          $("#keyword").val($("#service").text());
});

Cause #service is not gonna be available until your ajax request has completed.

EDIT: and as @tymeJV correctly said, it should be .val() on the textbox.

Upvotes: 1

beautifulcoder
beautifulcoder

Reputation: 11320

Try

$("#keyword").val($("#service").val());

Upvotes: 0

Related Questions