Вопрос

Is there a way to change the class value for all of the buttons with JS? right now I've got:

  var nodes = document.getElementsByClassName("yui-button yui-radio-button");

and then a for loop

  for (i=0;i<nodes.length;i++) {
     nodes[i].className = "something";
  }

is there anyway to bypass the for loop and change all the class values?

Here's the html:

    <div id="season" align="center">
     <div id="seasonButtons" class="yui-buttongroup">
      <span id="yui-gen0" class="yui-button yui-radio-button">
       <span class="first-child">
        <button type="button" id="yui-gen0-button">Spring</button>
       </span>
      </span>
      <span id="yui-gen1" class="yui-button yui-radio-button">
       <span class="first-child">
        <button type="button" id="yui-gen1-button">Summer</button>
       </span>
      </span>
      <span id="yui-gen2" class="yui-button yui-radio-button">
       <span class="first-child">
        <button type="button" id="yui-gen2-button">Fall</button>
       </span>
      </span>
     </div>
    </div>
Это было полезно?

Решение 2

you can do this with jquery

    $(".yui-button,.yui-radio-button").addClass("something").removeClass("yui-button yui-radio-button");

Другие советы

If you're just using plain javascript, then you need some kind of loop in order to change multiple nodes in the array.

You could write yourself a helper function that would operate on all the nodes in the array, but somewhere there's a loop that's going to run.

Since it looks like you're using YUI, YUI probably has ways of operating on a collection of nodes with one call.

Add a common class to all div. for example class="common"

then

$('.common').addClass('newClassName');

NodeList getElementsByClassName(string classNames):

Returns an array-like object of elements that have a class attribute that includes all of the specificied classNames.classNames.

If you want to work with javascript, you´ll need a loop to change the attributes of that nodes.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top