How to use javascript or jQuery to quickly select a preset of multi-select listbox items? -


Say that the index '1,3,4,5,12' and a multiple in my page- 12 items in it Select the list box containing.

What is a quick way to use Javascript to tell the list box to select all the items in those indexes?

How would this be done using jQuery?

For example, if the user associates the 'cannimal' preset with the 'candy' listboxes, then he will select all the candy bars of candy ... I think you get this idea.

This can do the trick:

  & lt; Select id = "select" multi = "multiple" & gt; & Lt; Option value = "1" & gt; Test 1 & lt; / Options & gt; & Lt; Option value = "2" & gt; Examination 2 & lt; / Options & gt; & Lt; Option value = "3" & gt; Examination 3 & lt; / Options & gt; & Lt; Option value = "4" & gt; Exam 4 & lt; / Options & gt; & Lt; Option value = "5" & gt; Exam 5 & lt; / Options & gt; & Lt; Option value = "6" & gt; Exam 6 & lt; / Options & gt; & Lt; Option value = "7" & gt; Exam 7 & lt; / Options & gt; & Lt; Option value = "8" & gt; Exam 8 & lt; / Options & gt; & Lt; Option value = "9" & gt; Examination 9 & lt; / Options & gt; & Lt; Option value = "10" & gt; Test 10 & lt; / Options & gt; & Lt; Option value = "11" & gt; Test 11 & lt; / Options & gt; & Lt; Option value = "12" & gt; Exam 12 & lt; / Options & gt; & Lt; / Select & gt;  

javascript (jquery):

  index = [1,3,4,5,9,12] $ (document) .ready (function ) {For (i = 0; i & lt; indexes.length; i ++) {$ ('# select option: eq (' + (indexed [i] -1) + ')'). Attr ('selected ',' Selected ');}});  

without jQuery:

  window.onload = function () {var indexes = [1,3,4,5,9,12]; Var option = document.getElementById ('Select'). the option; For (i = 0; i  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -