?
Solved

How to select Listbox item via Javascript Jquery

Posted on 2014-11-16
2
Medium Priority
?
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article discusses how to implement server side field validation and display customized error messages to the client.
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)
Suggested Courses

765 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