tags:

views:

40

answers:

4

Hi there,

I have a select element having values 10,25,50,100. Onclick of each i have to call a function . So can you tell me how can I access the value of a select element when clicked or selected and pressed enter.

Here is my code for select element:

<select name='rows'>
    <option name = 'ten' value='10'>10</option>  
    <option name='twofive' value='25'>25</option>
    <option name='fifty' value='50'>50</option>
    <option name='hundred' value='100'>100</option>
</select>

Thank you in advance.

A: 
$("select").change(function(){
 alert("Current val "+$(this).val());
});
Chinmayee
This assumes the OP is using, or is happy to use, jQuery.
chigley
can I use this in php code?
Rishi2686
Yes you can include jquery js in your page page. And then add this code inside script tags
Chinmayee
@Rishi2686: what do you mean? Do you want to use the values of the `<select>` in a php script? In that case you have to submit the form the `<select>` is in `onchange`. Or use Ajax.
captaintokyo
Thank you @chinmayee!!
Rishi2686
@captaintokyo , see i have to get clicked value from combobox, and have to pass it to ajax function, value from combobox will be rowperpage.
Rishi2686
A: 
<select id="example_select">
....
$("#example_select").click(function() {
    alert($("#example_select").val());
});

$("#example_select").change(function() {
    alert($("#example_select").val());
});
Alexander.Plutov
+1  A: 

This could be achieved by using Javascript

 echo "<select name=\'rows\' onChange='alertme(this.value)'>";  
 echo "<option name = 'ten' value='10'>10</option>";  
 echo "<option name='twofive' value='25'>25</option>";  
 echo "<option name='fifty' value='50'>50</option>";  
 echo "<option name='hundred' value='100'>100</option>";  
 echo "</select>";


<script>
function alertme(selectValue)
{
    alert(selectValue);
}
</script>
nik
A: 

You need to bind a function to the onchange event. Below is a functional html illustrating how it's done:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
    <script type = "text/javascript">
        function myfunc(combo) {    
            var selectedIndex = combo.selectedIndex;
            var selectedVal = combo.value;
            alert("Selected val: " + selectedVal + ", selected index: " + selectedIndex);
        }
    </script>
</head>
<body>
    <select name="rows" onchange="myfunc(this)">
          <option name = 'ten' value='10'>10</option>
          <option name='twofive' value='25'>25</option>
          <option name='fifty' value='50'>50</option>
          <option name='hundred' value='100'>100</option>
    </select>
<body>
</html>
Victor Ionescu