Keith
Keith

Reputation: 73

jquery write a style using variable

OK, once again I need help. I have managed to pass a variable in a query string from one page to another using this guys code: http://snipplr.com/users/Roshambo/

That all works great and I can see that it works by using an alert on the second page to test it:

var byName = $.getUrlVar('id');
alert(byName);

However... what I would like to do is take that var and paste it into a css rule, so that this would essentially happen:

div#byName{
    display: block;
}

I've read that using document.write is frowned heavily upon and to be honest, I just don't know what I'm doing. How can I get the div with an id the same as the var to display as a block?

Any help greatly greatly appreciated. I am at wits end with this project! :(

Upvotes: 1

Views: 1846

Answers (3)

Mauro
Mauro

Reputation: 2070

You can apply css style using the .css() jQuery command as

var byName = $.getUrlVar('id'); 
$("#" + byName).css("display", "block");

Look at: http://api.jquery.com/css/

or use addClass() and define your style in css file

var byName = $.getUrlVar('id');
$("#" + byName).addClass("example");

and

div.example{
    display: block;
}

Upvotes: 1

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

You can try:

var byName = $.getUrlVar('id');
$("#" + byName).css("display", "block");

Upvotes: 3

meder omuraliev
meder omuraliev

Reputation: 186562

Can you just getaway with .addClass('foo') where the class already has the style?

Upvotes: 0

Related Questions