views:

67

answers:

6

Is it possible to remove the attribute of the first html tag? So, this:

<div style="display: none; ">aaa</div>

becomes

<div>aaa</div>

from the following:

<div style="display: none; ">aaa</div>
<a href="#" style="display: none; ">(bbb)</a>
<span style="display: none; ">ccc</span>​

Many thanks in advance.

+2  A: 

Yes, in fact jQuery has something for this purpose: http://api.jquery.com/removeAttr/

Alex
A: 

This ought to do it:

$('some_selector_here').removeAttr('style'); 
Matt Ball
+2  A: 

To remvove it from literally the first element use .removeAttr():

$(":first").removeAttr("style");

or in this case .show() will show the element by removing the display property:

$(":first").show();

Though you probably want to narrow it down to inside something else, for example:

$("#container :first").removeAttr("style");

If you want to show the first hidden one, use :hidden as your selector:

$(":hidden:first").show();
Nick Craver
Your first example `$(":first")` doesn't limit itself to the content of the `body` tag, which I assume is the intention. http://jsfiddle.net/jqB2b/ Using `$('body :first')` works. http://jsfiddle.net/jqB2b/1/
patrick dw
@patrick - Right...hence the second part of the answer :)
Nick Craver
awesome. thanks, Nick.
DGT
@DGT - welcome :)
Nick Craver
Nick - OK, fair enough. :o) But still worth noting since the first part doesn't do what I assume the OP wanted given the markup in the question.
patrick dw
+3  A: 

You can use the removeAttr method like this:

$('div[style]').removeAttr('style');

Since you have not specified any id or class for the div, the above code finds a div having inline style in it and then it removes that style from it.

If you know there is some parent element of the div with an id, you can use this code instead:

$('#parent_id div[style]').removeAttr('style');

Where parent_id is supposed to be the id of parent element containing the div under question.

Sarfraz
It will remove the style from all divs, while the OP ask specifically for the first one ..
Gaby
@Gaby: I have also pointed out if there is parent element. For the first div `:first` filter selector will be needed as already pointed out :)
Sarfraz
@Sarfraz - I don't see `:first` anywhere in your answer...
Nick Craver
@Nick Craver: I meant in *your* answer, sorry should have clarified that :)
Sarfraz
thanks, Sarfraz.
DGT
@DGT: Welcome...
Sarfraz
A: 

You say "remove the attribute" — do you mean to remove all attributes? Or remove the style attribute specifically?

Let's start with the latter:

$('div').removeAttr('style');

The removeAttr function simply removes the attribute entirely.

VoteyDisciple
A: 

it is easy in jQuery just use

$("div:first").removeAttr("style");

in javascript

use var divs = document.getElementsByTagName("div");

divs[0].removeAttribute("style");

Shusl