Edoras Edoras - 1 month ago 12
Javascript Question

Hashtag in URL for show div base on compare ID

Ok, I will try explain my question.
I need to add class ".active" into already existing class ".jobs", when URL have hashtag same as ID, inside tag with class ".jobs".

Here is working code just for one compare:

$(document).ready(function() {
var hashVal = window.location.hash.split("#")[1];
if(hashVal == 'programmer') {
$("#programmer").addClass('active');
}
});


In practice:
Someone will come to website www.domain.tld/jobs#programmer then I need compare "#programmer" from ULR with all existing IDs in all tags, which also have class ".jobs", and if there will be someone with class="jobs" and also id="programmer", I need to add into this tag class "active".

Is there way to make jQuery code more variable? Like without having add comparison for each ID name? and also I need to move browser windows on position, where div with that ID it is.

Thanks

Answer
var hashVal = window.location.hash.split("#")[1];    
if($('#'+hashVal).hasClass('jobs')) $('#'+hashVal).addClass('active');