By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

select elements

P: 2
Hi gurus,

I have following code:
Expand|Select|Wrap|Line Numbers
  1. <select id="edit-field-color-tids" class="form-select" multiple="multiple" name="field_color[tids][]">
  2.     <option value="362">ffcedc</option>
  3.     <option value="363">ffdebe</option>
  4.     <option value="364">fff5c5</option>
  5.     <option value="365">e1ffd8</option>
  6.     <option value="366">bff7ff</option>
  7.     <option value="367">e7cdff</option>
  8.     <option value="368">f7f4ee</option>
  9.     <option value="369">ffffff</option>
  10.     <option value="370">ff8aab</option>
  11. </select>
And I'm curious is there any way to select elements in the select list with javascript help? Basically I would like get same result as I would get if I have used CTRL+click with mouse.

But I need to do that without actually pressing the select list.

I hope my explanation is clear,
looking forward and thanks in advance
Jan 5 '08 #1
Share this Question
Share on Google+
5 Replies


P: 1
If it is before you render the markup, add 'selected' in the items you want selected, e.g:

<option value="364" selected>fff5c5</option>

(Not JavaScript really, just HTML)

If the list is already part of your document tree you may need to alter it, i.e. locate the DOM element node of the option you need to select, and modify the innerHTML value. i usually assign a unique id to the whole list and use prototype.js framework $(<element id>).update(<new markup>).

Cheers!

Hi gurus,

I have following code:
Expand|Select|Wrap|Line Numbers
  1. <select id="edit-field-color-tids" class="form-select" multiple="multiple" name="field_color[tids][]">
  2.     <option value="362">ffcedc</option>
  3.     <option value="363">ffdebe</option>
  4.     <option value="364">fff5c5</option>
  5.     <option value="365">e1ffd8</option>
  6.     <option value="366">bff7ff</option>
  7.     <option value="367">e7cdff</option>
  8.     <option value="368">f7f4ee</option>
  9.     <option value="369">ffffff</option>
  10.     <option value="370">ff8aab</option>
  11. </select>
And I'm curious is there any way to select elements in the select list with javascript help? Basically I would like get same result as I would get if I have used CTRL+click with mouse.

But I need to do that without actually pressing the select list.

I hope my explanation is clear,
looking forward and thanks in advance
Jan 6 '08 #2

Expert 100+
P: 785
First, give a better name, or you will have BIG problems in javascript.
Expand|Select|Wrap|Line Numbers
  1. name="field_color[tids][]" 
is not acceptable! Use
Expand|Select|Wrap|Line Numbers
  1. name="field_color" 
only!

Then you can select elements in the list with following code, which for example selects the element at index 3 (=fourth element of box):
Expand|Select|Wrap|Line Numbers
  1. var selectBox = document.getElementById("field_color");
  2. selectBox.options[3].selected = true;
Jan 6 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Then you can select elements in the list with following code, which for example selects the element at index 3 (=fourth element of box):
Expand|Select|Wrap|Line Numbers
  1. var selectBox = document.getElementById("field_color");
  2. selectBox.options[3].selected = true;
...or use selectBox.selectedIndex = 3.
Jan 7 '08 #4

P: 2
Thank you guys for the help! You helped me out A LOT!
Jan 13 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Post again if you have any more questions.
Jan 14 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.