3

Using Prototype and Script.aculo.us, I am trying to make all tags with an ID starting with "idea" Droppable but I can't remember how to get all tags starting with a specific string. Thank you in advance.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Todd Kendall
  • 63
  • 1
  • 4
  • More information please. Please see the [FAQ](http://stackoverflow.com/faq) for info on how to write good questions. Posting code samples, and telling us more about what you are trying to do, is the best way to get a good, quality answer – jamesmortensen Jan 23 '11 at 06:45
  • What is "gatherElementById". You really need to make this question more clear. – jamesmortensen Jan 23 '11 at 07:08
  • I think Javascript needs more synonyms in its function names. Perhaps retriveElementById, fetchElementById, acquireElementById etc. – MooGoo Jan 23 '11 at 07:48
  • Is it so hard to understand? I added the frameworks to the question and rephrased it slightly. Todd, please let me know if that is what you meant – mplungjan Jan 23 '11 at 09:31

3 Answers3

4

The previous answers are correct, use a class name instead.

However if you are forced to use IDs there is a way with a CSS selector, now supported by most JS frameworks:

$$('*[id^=idea]')
clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • you were the innovator of this code and I did experiment and found it not working. After adding a little flavor, I made it working. However, all credits should be to you. – Nazmul Jan 28 '11 at 03:51
3

Give them a class and do $$('div.idea');

mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

Don't do that. IDs are meant to be used to get a single element.

The proper way to do what you want is giving those elements a common class. Then you can use whatever method prototype has to retrieve all elements with that class name (in jQuery it would be $('.class')). By using classes you allow your JS framework to use highly optimized code to search for matching elements (might even be a native getElementsByClassName method) instead of iterating over basically every single element and doing a substring comparison.

Edit: In prototype you can use $$('.class') to get all matching elements.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636