Solved

How to select Listbox item via Javascript Jquery

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

730 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