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

Web ApplicationsJavaScriptPHP

Avatar of undefined
Last Comment
ziffgone
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
hielo
Flag of Wallis and Futuna image

>>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

Avatar of berserkk
berserkk
Flag of Australia image

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.
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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

Avatar of secondv
secondv
Flag of United States of America image

I'm not sure if that's possible, why don't you want to use onChange? That's the easiest and simplest way.
Avatar of secondv
secondv
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo