Calling Javascript on a Dropdown list

G

Guest

I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?

<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";



<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");


var selectedStatus = statusList.options[statusList.selectedIndex].value;


}

</script>
 
G

Guest

C said:
I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?

Why do you think that you are doing something wrong? What happens when
you try to use the code? Do you get any error message? Have you enabled
Javascript error messages in the browser?
<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

The "javascript:" protocol is only used when you put Javascript in an
URL. The only reason that you don't get an error message when you use it
elsewhere, is that it becomes a label instead.

ddlStatus.Attributes["onchange"] = "SetDateFields();";
<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");

Have you checked that this is the actual id that the element gets in the
html code?
var selectedStatus = statusList.options[statusList.selectedIndex].value;

You are getting the value instead of the text.
 
L

Laurent Bugnion [MVP]

Hi,

C wrote:

<script language="javascript" type="text/javascript">

Additionally to Göran's remarks, the "language" attribute is deprecated.
You can safely remove it and use only the "type" attribute.

Greetings,
Laurent
 
G

Guest

As mentioned on the other forum, aside from the issues adressed by the
others, you are doing anything wrong. Try putting alert(selectedStatus) just
before the closing brace and you will see the value.
Peter
 
G

Guest

Hi there,

In addition to Goran's reply, please find working snippet below:

-- begin aspx code --

<asp:DropDownList ID="ddlStatus" runat="server"
AutoPostBack="false" OnChange="SetDateFields()">
<asp:ListItem Text="Status1" Value="Value1"/>
<asp:ListItem Text="Status2" Value="Value2"/>
<asp:ListItem Text="Status3" Value="Value3"/>
</asp:DropDownList>

<script type="text/javascript">
function SetDateFields()
{
var statusList = document.getElementById('<%=ddlStatus.ClientID %>');
var selectedStatus = statusList.options[statusList.selectedIndex].text;

alert('selected status is : ' + selectedStatus);
}
</script>
-- end aspx code --

Note you have to use control.ClientID to get the valid ID (ClientID contains
all the parent controls ids - if i didnt use it and moved dropdown list to a
Panel control it'd stop work). You may be wondering why i put OnChange
attribute to drop down list declaration. This is allowed (even if you get a
warning from schema validation) and it's called 'expando'.

Hope it's clear now


Milosz
 
G

Guest

Hi there,

In addition to Goran's reply, please find working snippet below:

-- begin aspx code --

<asp:DropDownList ID="ddlStatus" runat="server"
AutoPostBack="false" OnChange="SetDateFields()">
<asp:ListItem Text="Status1" Value="Value1"/>
<asp:ListItem Text="Status2" Value="Value2"/>
<asp:ListItem Text="Status3" Value="Value3"/>
</asp:DropDownList>

<script type="text/javascript">
function SetDateFields()
{
var statusList = document.getElementById('<%=ddlStatus.ClientID %>');
var selectedStatus = statusList.options[statusList.selectedIndex].text;

alert('selected status is : ' + selectedStatus);
}
</script>
-- end aspx code --

Note you have to use control.ClientID to get the valid ID (ClientID contains
all the parent controls ids - if i didnt use it and moved dropdown list to a
Panel control it'd stop work). You may be wondering why i put OnChange
attribute to drop down list declaration. This is allowed (even if you get a
warning from schema validation) and it's called 'expando'.

Hope it's clear now


Milosz
 
B

Bhaskardeep Khaund

Hi,

U can acheive that by writing

ddlStatus.Attributes.Add("onchange","javascript:SetDateFields()")


Bhaskar
 
L

Laurent Bugnion [MVP]

Hi,

Bhaskardeep said:
Hi,

U can acheive that by writing

ddlStatus.Attributes.Add("onchange","javascript:SetDateFields()")

This is the same as what the OP did

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

Additionally, had you read previous answers, you would have seen that
you should never use "javascript:", except in very very specific cases.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top