Solved

How to select Listbox item via Javascript Jquery

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 1 31
c# if statement weird reaction 3 29
Achieve json result 2 32
COnsume rest client 6 11
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
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…

758 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

20 Experts available now in Live!

Get 1:1 Help Now