0

I am using dropdown and I want to get value of selected option.

I am able to do that using jquery html() method.

But in case selected option value is containing html encodable character lets say it is &(ampersand.)

alert('html() : ' + $('#dropdown option:selected').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>

Then this & is encoded to &a m p;

How to treat this value as only text?

1
  • You can use, .text() in that case. Commented Dec 22, 2015 at 11:28

5 Answers 5

3

https://jsfiddle.net/programtheweba/8gohx47g/

By using Jquery text(), we will get only text and characters are not encoded.

alert('text() : ' +$('#dropdown').text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>

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

2 Comments

Really? You the one who asked the question. Even though there are multiple right answers, only your answer has got the upvote. Really great!!
I cant upvote my que/ans. so yes I am also surprised!
1

& is a special character in HTML (it's used for entities) and cannot appear as a normal character.
Your HTML is invalid (&x is not an entity), but the browser automatically corrects it to &amp; as it is parsed.
jQuery faithfully returns the fixed-up HTML when you ask it to.

Source

Hence use .text() instead of .html():

$('#dropdown option:selected').text();

You can also use .val() along with select element selector to get selected options text value:

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

3 Comments

Hey man. did you see OP's answer? Something is messing up here.
@AnoopJoshi: what is messing up??
@AnoopJoshi: we cant do anything other than flagging it for moderation :)
0

You can use jquery .text() instead:

console.log('text() : ' + $('#dropdown option:selected').text());//prints out text() : Test & Get knowledge
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropdown">
  <option selected>Test & Get knowledge</option>
</select>

Comments

0

You need to use .text() instead of .html():

$('#dropdown option:selected').text();

Or use .val()

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

Comments

0
  1. Include jQuery in your code.
  2. Wrap your code in DOM Ready $(function(){ ... })
  3. Use $('#dropdown option:selected').val() to fetch the value.

$(function(){
alert('html() : ' + $('#dropdown option:selected').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>

& will be parsed as & only and will not be changed.

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.