Edoras Edoras - 1 year ago 95
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') {

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.


Answer Source
var hashVal = window.location.hash.split("#")[1];    
if($('#'+hashVal).hasClass('jobs')) $('#'+hashVal).addClass('active');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download