Search code examples
javascriptprototypejs

How to select element by class in prototype?


I have this code below. I need to use a class id instead of the index to update the item. Say my class name was "initials" How would I use that to update the field instead of an index, just in case someone changed it?

 $$('select.employee_select').each( function(elm) {
            elm.observe('change', function(ev) {
                item = elm.childElements().find(function(ele){return !!ele.selected});
                item = item.readAttribute('value');
                elm.up().previousSiblings()[3].update(item); *** I want to change this line by class name instead of the index ***
                serv_id = elm.up().previousSiblings()[13];
                serv_id = serv_id.readAttribute('value');
                employee_id = elm.up().previousSiblings()[4];
                employee_id = employee_id.readAttribute('value');

Solution

  • You can refer to it in array notation:

    $$('select.employee_select .initials')[0] 
    

    or if it is the only element with that class:

    $$('.initials')[0]