David
David

Reputation: 237

How to hide an element defined by a variable

I have a variable itemid (which is the id of an element) which I want to hide, but firefox tells me this code is illegal:

  $(#(itemid)).hide();

Is what I am trying not possible or am I just tring it the wrong way? Also, in terms of debugging, is it possible to use javascript variable in alert boxes?

Thanks

Upvotes: 0

Views: 411

Answers (5)

Sparkup
Sparkup

Reputation: 3754

this will do it :

var itemid = '';

$('#'+ itemid ).hide();

Upvotes: 0

shaggy
shaggy

Reputation: 1718

Your code should be:

$("#"+itemid).hide();

Upvotes: 3

Amir Ismail
Amir Ismail

Reputation: 3883

Try This

  $('#'+itemid).hide();

Upvotes: 3

Jamiec
Jamiec

Reputation: 136074

try this:

$('#' + itemid).hide();

Upvotes: 7

Henry
Henry

Reputation: 222

Your selector syntax is incorrect. Try:

$("#" + itemid).hide();

EDIT

Fixed stupid mistake.

And to answer the second part of your question, yes it is.

var someVar = "hello world";
alert(someVar); // displays an alert box with the variable

Upvotes: 0

Related Questions