-3

How can I find the style attribute of a particular ID element?

E.g

desc = "#thisID";
desc_style = desc.attr('style');
console.log(desc_style);

Edit for clarification, above is an example.

Desc variable holds string #thisID.

How do I find the style attribute of that ID?

Francesca
  • 26,842
  • 28
  • 90
  • 153

4 Answers4

4

As mention in my comment desc is a string not an jquery object, so you can't use jquery methods. You can simple use:

$("#thisID").attr("style");
Alex Char
  • 32,879
  • 9
  • 49
  • 70
0

A jQuery object is returned with this syntax

desc = $("#thisID");

you can then get the style as you were doing

desc_style = desc.attr('style');
console.log(desc_style);

EDIT:

If you want to keep desc as a string, do this:

desc = "#thisID"
desc_style = $(desc).attr('style');
console.log(desc_style);
roryok
  • 9,325
  • 17
  • 71
  • 138
  • I have clarified in OP. The code was an example. #thisID is correctly stored in a string. How do I find the style attribute contents of that ID? – Francesca Dec 12 '14 at 11:50
0

If desc holding string #thisID, then

desc_style = $(desc).attr('style');
console.log(desc_style);

Or, if desc only holding thisID, then you need to concat # before it

desc_style = $("#" + desc).attr('style');
console.log(desc_style);
Bharadwaj
  • 2,535
  • 1
  • 22
  • 35
0

You could use something like:

("'"+desc+"'").css("styleToGet","settingItIsOptional");

if desc is a string value of #MyElementsID

If you just wanted to remove the desc altogether, you can use:

("#MyDivID").css("styleToGet","settingItIsOptional");

I believe getting the attr("style") may or may not be a better option here, but it depends on your requirements I suppose.

jbutler483
  • 24,074
  • 9
  • 92
  • 145