kentico - Switch an image according to the H2 text in Jquery -


i'm new whole jquery thing. i'm trying create website in kentico cms, want change header image according h2 header content. each different page gets different image.

i've tried .addclass, .css , .attr nothing seems work. jquery have tried following:

$(document).ready (function() { {  var title = $('h2').text();   switch(title) {     case 'about us':         $('.contentheader').addclass('contentabout')         break;     case 'education':         $('.contentheader').css('background-image','url(/edu.png)')         break;     case 'projects':         $('.contentheader').attr('src','/projects.png')         break;     case 'benefits':         $('.contentheader').attr('src','/benefits.png')         break;     default:  }    )}; 

so want change header edu.png if h2's text = education.

try change )}; }); last line, , if don't need default: , don't use it.

also remove { line 2 (thanks peter)

$(function() { // dom ready (shorthand)      var title = $('h2').text();       switch(title){         case 'about us':             $('.contentheader').addclass('contentabout');             break;         case 'education':             $('.contentheader').css('background-image','url(/edu.png)');             break;         case 'projects':             $('.contentheader').attr('src','/projects.png');             break;         case 'benefits':             $('.contentheader').attr('src','/benefits.png');             break;     }  }); 

you can way:

$(function(){     var title = $('h2').text();     var changes = {     'about us'  : ['addclass', 'contentabout'],     'education' : ['css',      {backgroundimage: "url(/edu.png)"}],     'projects'  : ['attr',     {src: "/projects.png"}],     'benefits'  : ['attr',     {src: "/benefits.png"}]   };    var method = changes[title][0];   var valu   = changes[title][1];    $('.contentheader')[method](valu);  }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -