Change id for all elements having a class

Duders

I am pretty new to JavaScript/web development in general. I am trying to create a JS function that takes an input string. That string will be the name of a class. I am trying to get all the elements of that class (they are all id's), and then add "OFF" to the end of all of the id's. I have been looking through existing threads, and found this one:

Give all elements in Class an seperate Id Javascript

However, I am still unable to get my function to work:

function idOFF (inputClass) {
    var x = document.getElementsByClassName("'" + inputClass + "'");
    for (i = 0; i < x.length; i++) {
        x.setAttribute('id', x[i] + "OFF");
    }
}

I have accomplished this on an individual level using jQuery, and tried incorporating that code into my function. It wouldn't work that way either:

function idOFF (inputClass) {
    var x = document.getElementsByClassName("'" + inputClass + "'");
    for (i = 0; i < x.length; i++) {        
        $("'#" + x[i] "'").attr("id", x[i] + "OFF");
    }
}
Ionică Bizău

If you're using , you can do the following:

function idOFF (inputClass) {
    var $elms = $("." + inputClass);
    $elms.forEach(function () {
        var $this = $(this);
        $this.attr("id", $this.attr("id") + "OFF");
        // or just this:
        // this.id += "OFF"
    });
}

You also can do that in pure , fixing some issues you have in your current code:

function idOFF (inputClass) {
    var x = document.getElementsByClassName(inputClass);
    for (i = 0; i < x.length; i++) {        
        x[i].id += "OFF";
    }
}

Note the getElementsByClassName where I removed the additional quotes and I also removed the jQuery wrapping replacing it with id.

  function idOFF(inputClass) {
    var x = document.getElementsByClassName(inputClass);
    for (i = 0; i < x.length; i++) {
      x[i].id += "OFF";
    }
  }

  idOFF("foo");
#testOFF {
  color: red;
}
<div class="foo" id="test">1</div>
<div class="foo" id="test1">2</div>
<div class="foo" id="test2">3</div>
<div class="foo" id="test3">4</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change content of all elements having same class using javascript

Iterate all elements and change their id

Select all the elements with the same class, with some having two classes

find and collect values of all elements having `data-id` attribute

how to display no.of elements having same data-id and class?

Change all <p> elements with a class to <code>

how to change all elements id in div child

Change all elements with id='s value

Add class to all elements with a specific ID jQuery

click on all elements by id or by class in jquery

javascript, can't change class on all elements of the same class

JQuery find and change class of all elements with data attribute

Change value of all elements with same class won't work

How to change class for all elements retrieved by document.getElementsByClassName

Change class of all ancestor li elements with PHP DOMDocument

find elements by class whose id contains a substring and change the style

How to select all elements that are not of a class within an id css

jQuery: select all elements of a given class, except for a particular Id

How to extract all div elements with a specific id and a specific class?

Get id's of all child elements with a particular class

Select all elements that share a class but do not have a specific id

Store to array the id of all elements with a given class name

Merge all elements from Two Lists having the same Date and Id and sum up their Costs

How to change background color of a specific 'td' element having no id/class, when it is clicked?

Change class of multiple elements

hide all elements with ID

Having trouble getting elements to change in HTML

Find elements with class and id

Objectify query by list having all elements of a sublist