I am trying to create an element with ID of the element class name…
my codes
//$elementClass has no id attrbute
elementID=$elementClass.attr('class');
$elementClass.first().attr('id',elementID);
$element=$(elementID);
console.log($element);
//the console.log doesn't output anything....
Can anyone help me about it? Thanks a lot.
Unless your class has the
#character in it, all you get is the class name value. You need to add the#character to your selector if you want to select the element with that id.However, seeing you are executing
first()already to get the element of interest you might as well cache that object at that point and use it from there on.That should work too and it saves you having to concatenate selector values.