0

I have a a drop down filed like below.

HTML and PHP

 <select id="selectform_0">
    <option value="0">1</option>
    <option value="1">2</option>
    <option value="2">3</option>
    <option value="3">4</option>
    <option value="4">5</option>
    </select>


<input type="button" onclick="selectValue(<?php echo $id; ?>)
">

And Javascript

<script>
function selectValue(id)
{
  //take select value
  alert(selectvalue);
}
</script>

is there anyway to take value from it by javascript strictly by not using <form>

Jhilom
  • 1,028
  • 2
  • 15
  • 33
  • possible duplicate of [How to get selected value of dropdownlist using JavaScript?](http://stackoverflow.com/questions/1085801/how-to-get-selected-value-of-dropdownlist-using-javascript) – Zoltan Toth May 14 '12 at 10:27

3 Answers3

1

Yes you can.

Markup

<select id='myselectform' name="selectform_0">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>

JS

   //If you want the values when an action takes place, say, onchange
    document.getElementById("myselectform").onchange = function(){
        alert(this.value)
    }
   //Or this way,
    var t = document.getElementById("myselectform");
    var selectedElem = t.options[t.selectedIndex].value;

   //Or if you just want the whole list of values then do something like this,
   var t = document.getElementById("myselectform").getElementsByTagName("option");

    for(var i=0,len=t.length; i<len; i++)
    {
      alert(t[i].getAttribute("value"))
    }
Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49
1

Try this

Javascript

<script>
function selectValue(id)
{ 
var obj = document.getElementById("selectform_"+id).options[document.getElementById("selectform_"+id).selectedIndex].value;
alert(obj);
}
</script>

HTML

<select id="selectform_0">     
<option value="0">1</option>     
<option value="1">2</option>     
<option value="2">3</option>     
<option value="3">4</option>     
<option value="4">5</option>     
</select> 

<input type="button" onclick="selectValue(<?php echo $id; ?>) "> 
Jhilom
  • 1,028
  • 2
  • 15
  • 33
-1

you can use jQuery

$("#myselectform").val();

or

$("#myselectform").attr("selected");
unarity
  • 2,339
  • 2
  • 21
  • 17