javascript - jQuery check if element has a class -


i trying check see if html element has class , if so, run function jquery. code works if element doesn't have multiple classes. believe need use .hasclass() method, couldn't figure out.

var pageclass = $("body").attr('class'); switch (pageclass) {   case ("page1"):     $('h1').html('heading1');     break;   case ("page2"):      $('h1').html('heading2');     break;   default:     $('h1').html('default'); } 

fiddle: https://jsfiddle.net/9o70dbzz/2/

this can solved selectors:

$('h1').html('default'); $('body.page1 h1').html('heading1'); $('body.page2 h1').html('heading2'); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -