Solved

How to select Listbox item via Javascript Jquery

Posted on 2014-11-16
2
441 Views
Last Modified: 2015-01-08
I have a asp.net ListBox control on a page, that has nothing selected.

I will display a generated TABLE that includes a Javascript ONCLICK event to call a JS function to select that item.

Generated onclick event for the row:
onclick=\"addListbox('" + row.Channel_ID.ToString() + "')"

Open in new window


the JS:
<script type="text/javascript">
    function addListbox(selectedItem)
    {
        $("#ddChannels option[value='" + selectedItem + "']").attr("selected", true);
    }
</script>

Open in new window


Just for reference, the listbox:

<asp:ListBox ID="ddChannels" runat="server" CssClass="chzn-select" SelectionMode="Multiple"></asp:ListBox>

Open in new window


This ListBox is populated by the code behind...

But it doesn't seem to be working.
0
Comment
Question by:adworldmedia
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40446073
try this :
<script type="text/javascript">
    function addListbox(selectedItem)
    {
        //$("#ddChannels option[value='" + selectedItem + "']").attr("selected", true);
        $("#ddChannel")val(selectedItem);
    }
</script>

Open in new window

0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 40447232
There are 2 ways of doing this thing.

<script type="text/javascript">
    function addListbox(selectedItem)
    {
        $("#ddChannels option[value=" + selectedItem + "]").attr("selected", "selected");
        
    }
</script>

Open in new window


Or

<script type="text/javascript">
    function addListbox(selectedItem)
    {
        $("#ddChannel").val(selectedItem);
        
    }
</script>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now