How to get elements with an identifier containing certain text? (Prototype)

I have a set of 3 radio buttons with these element IDs:

id='details_first'
id='details_second'
id='details_third'

How to get all the elements whose identifier begins with 'details_'?

EDIT

What I'm actually trying to do is: Each radio button has an associated div that I want to display when the radio button is clicked. At one time, only one radio button was active, so you only need to show the div for this radio button (all others should be hidden). So the code for this is what I have: (sorry, this is a little wider):

<input id="details_first" name="details" type="radio" value="first"  onclick="$('details_*').hide();$('details_first_div').show();" />First<br/>
<div id="details_first_div" style="display:none">div for details_first</div>

<input id="details_second" name="details" type="radio" value="second"  onclick="$('details_*').hide();$('details_second_div').show();" />Second<br/>
<div id="details_second_div" style="display:none">div for details_second</div>

Or is there a better best way to do this? (I use Ruby on Rails). Maybe by selecting an attribute name- but how?

+3
1

, . , <input> name == details.

div <form>, id, , clicked element id + _div.

, .

. .

var form = $("FORM_ID");
form.observe( 'click', function(event)  {
  var el = event.element();
  if ( el.name == "details" ) {
    // select: #FORM_ID div#details_*
    idStartsWith("details_", "div", form).invoke("hide");
    $(el.id + "_div").show();
  }
});

/**
* Selecting elements by how their id starts
*
* @param text - starting of the id
* @param tag  - filter results by tag name [optional]
* @param root - root of the selection      [optional]
*/
function idStartsWith(text, tag, root) {
  root = root || document;
  tag  = tag || "*";
  var arr = root.getElementsByTagName(tag);
  var len = arr.length;
  var ret = [];
  for ( var i = 0; i < len; i++ ) {
    if ( arr[i].id.indexOf( text ) === 0 )
      ret.push(arr[i]);
  }
  return ret;
}
+2

Source: https://habr.com/ru/post/1770369/


All Articles