-3

I have a very basic question. I want to select the SELECTED text and dropdown value from the dropdown and show in the alert box.

My attempt:

Dropdown

<p id="test">
  Select a draft:
  <select id="Select" name="Select">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
</p>

JS

$("#Select").change(function()
{
    alert($(this).val()); // IF THIS WORKS FINE THEN NEXT LINE CODE SHOULD WORK TOO
    alert($(this).text()); // WHY THIS SHOWS ALL THE DROPDOWN TEXTS
    alert($("#Select option:selected").text()); // THIS JUST WORKS FINE
});

Question 1: What $(this) signifies? If it's signifies selected element then it should show the text also when doing $(this).text(). BUT IT DOESN'T work as expected.

Question 2: If I need to select the value and text of the dropdown is above mentioned is the efficient way to go about it.

Please guide me.

My JSFIDDLE Attempt

Unbreakable
  • 7,776
  • 24
  • 90
  • 171

8 Answers8

3

In your change event handler, $(this) is the <select> element.

The element represented by $(this) depends on the context where it is used.
It usually is the element which triggered an event (like here), or the element targeted by the iteration of an .each() loop, for example.

When an <option> is selected, the <select> take its selected option value as a value...
It doesn't do it for the text of the selected option.

So that is why the second alert() statement doesn't work.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
2

The keyword this in your example represents the element on which the event triggered. This means the select element. .text() return all text included in the element, so it gives all elements. .val() returns the value of an input, in this case it will return the value of the select, but beware as it does not return more than one value if you set mutiple=true.

Since we now know that .text() returns the text, and this is the input that changed, we can deduct that you'd prefer using .val() to get the value as it may differ from the display text.

alert($(this).find("option:selected").val());
Salketer
  • 14,263
  • 2
  • 30
  • 58
2

$(this) is used to make the this object a JQuery object which includes some extra functionality.

You can try this if you want to get the selected item value and text:

$(this).find(":selected").val(); // Gets the value of the selected option, if the value attribute in the option element is null it will give you the text

$(this).find(":selected").text(); // Gets the text of the selected option
bcngr
  • 745
  • 6
  • 13
1

hi the 'this' part is the raw DOM element from javascript $(this) makes it a jquery object, so you can use jquery. In this case it's the select.

If I need to select the value and text of the dropdown is above mentioned is the efficient way to go about it. Yes it's fine. jQuery get specific option tag text

Richard Housham
  • 1,525
  • 2
  • 15
  • 31
1

Answer to question 1:

$(this) means that you pass this to the $ function. In other words, you create a jQuery object from the this object. In your context, this refers to the elements matching the #Select selector: your select element.

$(this).text() is working normally because internally it calls innerText on the select tag: which contains the DOM code of your select and innerText contains all the text (not HTML) of the children of the select.

Answer to question 2:

To retreive the label of the selected option: $("#Select option:selected").text()

To retreive the value of the selected option: $("#Select option:selected").val()

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
1

You can alter what you already have written by changing this:

 alert($(this).val() + $(this).text());

to:

 alert($(this).val() + $("option:selected", this).text()); 

And overall giving you the code you already have.

$("#Select").change(function()
 {
     alert($(this).val() + $("option:selected", this).text());

     alert($("#Select option:selected").text());
});

$(this) is just selecting the identified object that you are choosing to use 'change' on which in this case is the select box.

Ryan
  • 368
  • 1
  • 10
  • 28
1

$("#Select").change(function() binds the function defined after this point to the HTML element <select id="Select" name="Select">. Therefore within that function, $(this) refers to that Select element. The information within that select element is: <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option>. This is why $(this).text() will give you all the options; you're asking for all the text inside that element.

($this).val() gets you the value of the overall select element, which is the text of the currently selected option.

anandsun
  • 186
  • 6
1

this refers to the context which was used to invoke the function. When you change the select option, this refers to the whole select dropdown.

If you look below, when I print the value of $(this).val, it gives the function which returns the value of the selected option.

Whereas, when I print the value of $(this).text, it gives the function which gives the whole select dropdown inner text.

To answer your second question, I think $(this).val() is more efficient as by using $(this) will always refer to the context which invokes the function. Thus, you can create modular code using it, by separating the use of anonymous function into a named function and using it for other select dropdown in your site, if you want in the future.

$("#Select").change(function()
{
    console.log($(this).val);
    
    console.log($(this).text);
    console.log($(this).val()); // IF THIS WORKS FINE THEN NEXT LINE CODE SHOULD WORK TOO
    console.log($(this).text()); // WHY THIS SHOWS ALL THE DROPDOWN TEXTS
    console.log($("#Select option:selected").text()); // THIS JUST WORKS FINE
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="test">
  Select a draft:
  <select id="Select" name="Select">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
</p>
Anurag Singh Bisht
  • 2,683
  • 4
  • 20
  • 26