473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calling Javascript on a Dropdown list

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>

Feb 2 '07 #1
7 33814
C wrote:
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.
}

</script>

--
Göran Andersson
_____
http://www.guffa.com
Feb 2 '07 #2
Hi,

C wrote:

<snip>
<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
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 2 '07 #3
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

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"C" wrote:
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>
Feb 2 '07 #4
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
"C" wrote:
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>
Feb 2 '07 #5
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
"C" wrote:
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>
Feb 2 '07 #6
Hi,

U can acheive that by writing

ddlStatus.Attributes.Add("onchange","javascript:Se tDateFields()")
Bhaskar

"C" <C@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...
>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>

Feb 3 '07 #7
Hi,

Bhaskardeep Khaund wrote:
Hi,

U can acheive that by writing

ddlStatus.Attributes.Add("onchange","javascript:Se tDateFields()")
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
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
1
by: Rod Early | last post by:
I need to know when the select element's dropdown list is opened (as when the user clicks on the arrow or does ALT-downarrow from the keyboard). Similarly, I need to known when the dropdown list...
6
by: passion_to_be_free | last post by:
This is probably simple, but I can't seem to find it anywhere. I have have some values stored in javascript variables. I have a <select> dropdown list whose options correspond to these values. I...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
3
by: RJN | last post by:
Hi The texts in the dropdown are too long and the width is not sufficient to show the entire text. Increasing the width is not an option. Is there a way to show the selected item text as a...
4
by: Dica | last post by:
i've got a dropdown list with various options added to it. i want to add a button that allows the user to "Add New Option' which should create a blank row on the dropdown list and allow the user to...
5
by: jung_h_park | last post by:
From: jung_h_park@yahoo.com Newsgroups: microsoft.public.dotnet.framework.aspnet Subject: Dropdown List not retaining its SelectedValue Date: Mon, 26 Jun 2006 21:02:57 -0700 Hello, My...
11
by: eureka | last post by:
Hi All, I'm training in Servlets, JSP and JavaScript, I have a web page in which there's a "StudentName" textbox and below it is a "Names" Dropdown list. Initially the Textbox is empty and...
1
by: bappadiit | last post by:
I am not sure that what I name it but here is the details of what I want to do: There will be two or more dropdown lists, suppose first one contains faculty name and the 2nd one is course name....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.