2

I'm just wondering if this is possible, supposed I have:

<select name = 'region' id = 'region'>
  <option value = '1'>Region 1</option>
<select>

Now, I know I get the value of 1 when I select "Region 1". Is there a way to get the "Region 1" as the value itself without changing the value = '1'. I need that for javascript for other dropdowns.

Sorry I forgot to mention, I'm referring to PHP. I know that:

$value = $_POST['region'];

will the value of 1, how can I get just the text to pass on $_POST?

Yassi
  • 2,379
  • 4
  • 24
  • 33

9 Answers9

1
var el = document.getElementById('region');
var text = el.selectedIndex == -1 ? null : el.options[el.selectedIndex].text;
console.log(text);
VisioN
  • 143,310
  • 32
  • 282
  • 281
Marin Vartan
  • 572
  • 7
  • 16
1

Cross Browser:

var select = document.getElementById('region');
if(select.addEventListener) {
    select.addEventListener('change', function(evt) {     
        var target = evt.target || evt.srcElement;
        var selected = target[target.selectedIndex];
        var text = selected.textContent || selected.innerText;
        alert(text);
    });
 }
else if(select.attachEvent) {
  select.attachEvent('onchange', function(evt) {
        var target = evt.target || evt.srcElement;
        var selected = target[target.selectedIndex];
        var text = selected.textContent || selected.innerText;
        alert(text);
    });
}

http://jsfiddle.net/LrETq/

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • I prefer your example over the other ones. Just one thing to note: `textContent` isn't compatible with all browsers, some of them (IE8) support `innerText` instead. – W.D. Apr 25 '14 at 09:07
  • @W.D thank you. Good point. I changed it check for at least one of them. – Ryan Apr 25 '14 at 09:09
  • You're welcome. Could you please correct your `innerText` and change it to `selected.innerText`? And I think `event.target || event.srcElement` will do it, no need for `event.originalTarget`. – W.D. Apr 25 '14 at 09:11
  • Just did. You know what I just realized a way shorter way to do this: `this[this.selectedIndex].innerText || this[this.selectedIndex].textContent;` When would that not be the best to do it? – Ryan Apr 25 '14 at 09:18
  • Using `this` with `.addEventListsner` isn't a good idea. IE8 won't recognize `this`. Besides, `.addEventListsner` isn't supported in IE8 as well, you may want to use `.attachEvent` instead. And one last 'remark' (: - change `var selected = event.target` to `var selected = target`. – W.D. Apr 25 '14 at 09:24
  • Yes, forgot to do that. Updated the fiddle. – Ryan Apr 25 '14 at 09:26
  • 1
    And `event` isn't supported in `Firefox`, change it to `evt = event || window.event; var target = evt.target || evt.srcElement;` – W.D. Apr 25 '14 at 09:34
1

Javascript:

var value1 = document.getElementById("region");
var value2 = value1.options[value1.selectedIndex].text;
alert(value2);
anik4e
  • 493
  • 8
  • 16
1

js

$("#region").change(function(){
  var domNode = document.getElementById("region");
  var value = domNode.selectedIndex;
  var selected_text = domNode.options[value].text;
  alert(selected_text);
});
1

First of all, as you haven't mentioned whether you are planning on supporting legacy browsers or not, I've decided to add support for that browsers as well. My script works with all IE versions (IE7 inclusive).

So, first we attach our eventlistener to your select element. Then we retrieve the text of the selected option and return the value;

Have a look at this => DEMO

If you want to submit it via $_POST than do the following ->

  1. Create a hidden input element, set its name attribute to say select, then set its value to the value of our text variable (we will have that variable when one of our options is selected) - See the code below.
  2. After submitting your form you can retrieve the value like so => $_POST['select'] (select is the name attribute we have assigned to our hidden input element)

Javascript

//attaching the eventlistener (modified the code to make it compatible with older IE versions)

function attach(element,listener,ev,tf){

    if(element.attachEvent) {

        //support for older IE (IE7 inclusive)
        element.attachEvent("on"+listener,ev);

        }else{

        //modern browsers
        element.addEventListener(listener,ev,tf);

        }

    }

function returnTextofTheSelectedElement(sel){

    //getting and returning the text of the selected option
    selectedIndex = sel[sel.selectedIndex];
    return text = selectedIndex.innerText ? selectedIndex.innerText : selectedIndex.textContent;

    }

var select = document.getElementById('region');

attach(select,'change',function(){

    //pass the select tag you want to get the text of
    //*returnTextofTheSelectedElement* function returns our text
    alert(returnTextofTheSelectedElement(select));

    //so you can store it in a *variable* and use it when submitting your form
    text = returnTextofTheSelectedElement(select);

    //if you want to submit it via $_POST than do the following
    //create a hidden *input* element, set its name attribute to say *select*, then set its value to the value of our *text* variable

    input = document.createElement('input');
    input.type = 'hidden';
    input.name = 'select';
    input.value = text;

    select.parentNode.appendChild(input);

    alert('The value/text of the hidden input is '+input.value);

    //when submitting the form, it will also submit out hidden input with the value (text) of the selected option
    //you can retrieve the value like so => *$_POST['select']* 


},false);

HTML

<select name='region' id='region'>

    <option value='1'>Region 1</option>
    <option value='2'>Region 2</option>
    <option value='3'>Region 3</option>

<select>
W.D.
  • 1,033
  • 1
  • 7
  • 12
0
$(select#region option).click(function(){

          var textOfTheSelectedOption= $(this).text();
          alert( textOfTheSelectedOption);

});
Tuhin
  • 3,335
  • 2
  • 16
  • 27
0

You can get text value like this. Is it helpful?

var e = document.getElementById("region");
var value = e.options[e.selectedIndex].text;
console.log(value);
Sardor I.
  • 416
  • 4
  • 8
0
<script>

    function getSelectedText(){
   var theSelect= document.getElementById("region");
var theText= theSelect.options[theSelect.selectedIndex].text;
alert(theText);

}
</script>
<select name = 'region' id = 'region'  onchange="getSelectedText()">
  <option value = '1'>Region 1</option>
  <option value = '2'>Region 2</option>

<select>
Tuhin
  • 3,335
  • 2
  • 16
  • 27
0
<form method="post" action="action.php">
<select name = 'region' id = 'region'  onchange="fnc()">
  <option value = '1'>Region 1</option>
  <option value = '2'>Region 2</option>
<select>
<input type="hidden" name="hidden_region" id="hidden_region">
<input type="submit" value="Send">
</form>
<script type="text/javascript">
function fnc(){
var el = document.getElementById('region');
var text = el.selectedIndex == -1 ? '' : el.options[el.selectedIndex].text;
document.getElementById('hidden_region').value = text;
}
</script>

in php post will be like

$value = $_POST['hidden_region'];
Marin Vartan
  • 572
  • 7
  • 16