2

I am trying to show the selected value of a drop down, but its not selected the dropdown value, but adding another value to the dropdown list.

here is the code I am using:

jquery

var companyId = $(this).closest('tr').find('#headquarterIdForSearch').text();
                var companyName = $(this).closest('tr').find('#companyNameId').text();
                $(this).parents().find('.hqSelect option:selected').val(companyId).text(companyName);

The above code is not selecting the value that I am assigning to the Text and val property. However, it is adding a new property. Lets say, I have fetched value for companyName as "FIS" and companyId as 123.

The value is present under the dropdown however, it is adding a new value to the dropdown.

How can I show the selected value? I have also used the below code:

$('#dropDownId :selected').text();

Thanks,

1
  • 1
    add your html dropdown list code Commented Aug 25, 2016 at 9:47

3 Answers 3

2

alert($('#id').val());

function demo()
{
  alert($('#id').val());

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='id' onchange="demo()">
  <option value="1">Vijay  </option>
  <option value="2">Raj  </option>
  <option value="3">Ganesh  </option>
  <option value="4">Ram  </option>
  </select>

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<option value="one">One</option>

$('#dropDownId').val();

It will give the selected option value ie. value="one" from above code snippet.

Comments

0

Just use .val() function to get the value, you don't need to pass any argument.

$(this).parents().find('.hqSelect').val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.