My Goal: I am attempting to dynamically assign a different class to the div element "logoswap" based on the current URL (e.g., foo.com/item1).
Below is the code I have put together, but it appears to be flawed. While I am more proficient in XHTML/CSS and have some experience with PHP, I am not an expert in JavaScript. Unfortunately, I cannot utilize PHP for this task even though I know it could be done with PHP. I have a PHP version that accomplishes what I need, but I am struggling to integrate the PHP functionality properly.
Essentially, I am trying to display a different logo depending on the directory or URL. The image does not necessarily have to be called in as a background element using a CSS class, I just need it to change based on the URL parameter mentioned above...
$(function() {
var url = location.pathname;
if(url.indexOf('item1') > -1) {
document.getElementById("logoswap").className += " class1";
}
elseif(url.indexOf('item2') > -1) {
document.getElementById("logoswap").className += "class2";
}
elseif(url.indexOf('item3') > -1) {
document.getElementById("logoswap").className += "class3";
}
elseif(url.indexOf('item4') > -1) {
document.getElementById("logoswap").className += "class4";
}
elseif(url.indexOf('item5') > -1) {
document.getElementById("logoswap").className += "class5";
}
else {
document.getElementById("logoswap").className += "class1";
}
});
This is my current implementation which may not be the most aesthetically pleasing.
I have reached out because I require assistance in improving it.