Link to home
Start Free TrialLog in
Avatar of brianbish
brianbish

asked on

get dropdown list selected value without using an event like onchange or onclick

How can I get the selected value of a dropdown list without using an event
///manage_products.php
 
<form  action="manage_products1.php" name="productForm" method="post" enctype="multipart/form-data">
 
<table>
 
                                  
 <div>Catergory:
<select name="productType" id="cat" /><?php echo pop_list("category");?>  </select></div>
 
<script language="javascript">
var var1=document.getElementById("cat").value;  /////how can I get the selected value from "cat" into var1?//this is not working
</script>  
 
<?php
function pop_list($boxname){
 
if($boxname=="category"){
$sql = "SELECT *FROM category";
$result=mysql_query($sql);
while ($dropdown = mysql_fetch_array($result) )
{
echo("<option value= ".$dropdown["id"]." >  ".$dropdown["name"]." </option>");
}
}
?>

Open in new window

Avatar of ziffgone
ziffgone
Flag of Canada image

Here is one way:

<script language="javascript">
   var elem=document.getElementById("cat");
   var sel=elem.selectedIndex;
   var val=elem.options[sel].value;
   alert(val);
</script>

Open in new window

Avatar of hielo
>>without using an event
I have no clue where you going with this. Why don't you explain what you are trying to do? The whole point of the event handlers is to do "stuff" whenever something happens on the browser. That "something" is triggered by the user. So you know what the user is doing based on the event triggered: click, onchange, etc.
If you had <select id="theList">, then this:

<script type="text/javascript:>
window.onload=function(){
 alert( document.getElementById("theList").selectedIndex )
}
</script>
will alert the selected option as soon as the page finishes loading. But not, window.onload IS an event. So, if you want to detect to execute code, you will need an event handler of some sort. It does NOT HAVE to be onclick or onchange. Other options are onmousemove, onmouseover, onmoouseout.

Open in new window

is there an item selected?
you can try a few different things, wait for the window.onload event
window.onload= function(){
 var var1=document.getElementById("cat").value;
};
or on older browsers you might use:
var sel = document.getElementById("cat");
sel.options[sel.selectedIndex].value;

but unless one of your select items is specifically designated as selected it might not work.
I completely missed that javascript comment. You need to do so onload - after the page has finished loading.
<script language="javascript">
var var1=null;
window.onload=function(){
 var1=document.getElementById("cat").value;  /* how can I get the selected value from "cat" into var1?//this is not working */
alert( var1;
}
</script> 

Open in new window

I'm not sure if that's possible, why don't you want to use onChange? That's the easiest and simplest way.
Hmm, well, several replies :) Ignore me comment about not being possible - but still would like to know why wouldn't you want to use an event like onChange
ASKER CERTIFIED SOLUTION
Avatar of ziffgone
ziffgone
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial