Sunday, 20 November 2016

how do i add a class to my h1 with javascript?










I'd like to have JavaScript add a class to my h1 elements automatically.




How would I add JavaScript to replace the h1 element with h1="classs" etc?


Answer



Using Jquery it's simple. Loop all of the h1 elements adding the classname to it



$(function(){
$('h1').each(function(){
$(this).addClass('classname');
});
});



Or if you want to add a class per specific element ID



  $(function(){
$('#eleID').addClass('classname');
});

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...