0

I have search a lot but google just return me how to get the selected value of a select element

$('#some_id').select(function() { 
    // how can i store the selected value in a variable.?
    alert('the selected value');
});
Krupesh Kotecha
  • 2,266
  • 2
  • 17
  • 39
Mer Igos
  • 23
  • 4

3 Answers3

0

You can use change event to get selected value. In the event handler you can use $(this).val() of this.value to get the selected value.

$('#some_id').change(function(){
  // how can i store the selected value in a variable.?
  v = this.value;

  alert('the selected value' + this.value);
});
Adil
  • 139,325
  • 23
  • 196
  • 197
  • i mean. when i highlight a text in a text box, textarea or where ever that is, but its selected using jquery with a select() "$('#id').select()" the highlighted or selected text can be used to evaluate or for any purpose i need to. am i clear sir.? – Mer Igos Oct 29 '15 at 12:05
  • Are you looking to get selected text, http://stackoverflow.com/questions/717224/how-to-get-selected-text-in-textarea – Adil Oct 29 '15 at 12:17
0

Use .change() event. Something like this:

var current_value = 1;//can define any value null also
$('#some_id').change(function(){
    current_value = $(this).val();//storing
    alert(current_value);
});

Note: if you want to store value in variable and use it anywhere else then you must define it globally like i did.

DEMO

Manwal
  • 22,117
  • 10
  • 57
  • 89
  • i mean. when i highlight a text in a text box, textarea or where ever that is, but its selected using jquery with a select() "$('#id').select()" the highlighted or selected text can be used to evaluate or for any purpose i need to. am i clear sir.? – Mer Igos Oct 29 '15 at 12:05
  • So you don't have `select` but you have `input` & `textarea` and you want highlighted text. – Manwal Oct 29 '15 at 12:32
  • Have you tried http://stackoverflow.com/questions/717224/how-to-get-selected-text-in-textarea it has all details – Manwal Oct 29 '15 at 12:37
  • but its using DOM.? do they have any difference.? – Mer Igos Oct 29 '15 at 12:49
  • Most jquery event uses `DOM`. You can use that if you want to get highlighted text. – Manwal Oct 29 '15 at 12:51
0

you can try like this:

 $('#select_id').change(function(){
   var sval = $('#select_id').val();
   alert('the selected value' + sval);
});
shemy
  • 573
  • 1
  • 5
  • 15
  • i mean. when i highlight a text in a text box, textarea or where ever that is, but its selected using jquery with a select() "$('#id').select()" the highlighted or selected text can be used to evaluate or for any purpose i need to. am i clear sir.? – Mer Igos Oct 29 '15 at 12:05