Solved

How to select Listbox item via Javascript Jquery

Posted on 2014-11-16
2
453 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)

770 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