Tuesday, December 6, 2016

JavaScript DOM Manipulation - jQuery - CSS - Q&A

Question:

Write code that shows how to give a class to all X tag elements in an HTML page using CSS | JS | JQUERY

Answer

// CSS

div{

    background: rgba(0, 180, 0, .3);

    width: 150px;

    height: 150px; }


// JavaScript

var divs = document.getElementsByTagName('DIV');

for (i in divs){

    divs[i].className = "newDivClass";

};


// jQuery

var divs = $('DIV');

$.each(divs, function(index){

    divs[index].className = "newDivClass"

});


No comments:

Post a Comment