Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i would want to pass the selected text from a dropdown list into a javascript function
i tried using document.getElementById('DropDownList1').value but its throwing an error.

please assist

thanks
Joe
Posted
Updated 10-Oct-11 4:01am
v2

Hi,

Here I'm providing some for your requirement

just check this once

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script language ="javascript" >
        $(document).ready(function (evt) {
          
        });
        function f1(tid) {
            alert(tid.value);
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txtname" onkeyup="f1()" />
        <br />
        <select name="DropDownList1" id="DropDownList1" onchange="f1(this)">
            <option value="dfgdfg">fdgdfgdf</option>
            <option value="dfgdfgdfgdfdfg">fdgddfgdfgfgdf</option>
            <option value="dfgdfgdfgdfgdfggdfg">fdgdfgdfgdfgdfgdf</option>
        </select>
    </div>
    </form>
</body>
</html>


All the Best
 
Share this answer
 
Comments
joe_j 11-Oct-11 1:27am    
hi,
tried it, but its not picking up the selected dropdown text once the submit button is fired
Muralikrishna8811 11-Oct-11 4:19am    
can you post your sample design code
joe_j 13-Oct-11 5:44am    
hi Murali,
its below
that is the submit button
<asp:Button ID="Button1" runat="server" Text="Confirm" onclientclick="return askConfirm()" />

and below is the javacode
function askConfirm() {
var ddl = document.getElementById(DropDownList1).value;
// var SelectedIndex = ddl.selectedIndex;
// var SelectedValue = Dddl.value;

if (confirm("Confirm action ?" + ddl))
//alert("you clicked yes")
return true;
Muralikrishna8811 13-Oct-11 5:55am    
try this
function f1() {
if (confirm('are you sure ? ' + $("#DropDownList1").val())) {
return true;
}
else {
return false
}
}
Muralikrishna8811 13-Oct-11 5:56am    
use jquery to access server control in easy way
try this:
JavaScript
var selIndex = document.getElementById('ddlCountry').selectedIndex;
document.getElementById('ddlCountry').options[selIndex].value
 
Share this answer
 
Comments
joe_j 10-Oct-11 10:21am    
hi,
nope, its still throwing a jquery error
MZwahlen 11-Oct-11 1:28am    
but this is done without any jQuery code, so you shouldn't see any jquery error
Try to remove all jquery stuff
joe_j 11-Oct-11 1:43am    
i have to enter the above withing a <script type="text/javascript"> right

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900