1

I am absolutly new in JavaScript and I have the following problem.

I have a script that is automatically performed when the user click on a button.

I need that this script do also the following operations:

  1. Select an element into my DOM that have a specific ID

  2. This element have setted an inline CSS: style="display: block;", which have to be removed.

How can I do it?

3
  • in jQuery: $("#id").hide() Commented Apr 30, 2015 at 13:50
  • You should use addClass and removeClass in jQuery if you want your CSS to control the show/hide Commented Apr 30, 2015 at 13:50
  • use $('#element').css('display', 'inline'). Commented Apr 30, 2015 at 13:54

6 Answers 6

3
$("#id_of_the_element").hide();

or

$('#id_of_the_element').css('display', '');
Sign up to request clarification or add additional context in comments.

1 Comment

mmm what do the hide method? Hide an element? I don't need to hide the element, I simply have to remove the CSS settings because it is the cause of an anomaly
1

To remove any property, use :

  $("#id").css("the_css_property","");

Comments

1

If you're using vanilla javascript (that is without any library), do the following:

var element = document.getElementById( YOUR_ELEMENT_ID )
element.style.display = ''

You effectively just enter the attribute you want and set it to an empty string.

Comments

1

You can use .removeAttr():

$("#id").removeAttr("style");

Exemple

6 Comments

removeAttr only work with html attributes, not with CSS attributes. In your example "style" is an inline html attribute, but maybe he wants to delete a css property from a .css file. For that purpose, that doesn't work.
That is indeed what I understand of the OP's needs, removing html attribute style
but $.removeAttr only works in that situation. This time it works, but maybe not in every situation.
Are we talking about another situation that the one established in the question ?
You're right. If only one inline css attribute must be deleted but not all, I agree with you :)
|
0

With just JavaScript

<script>
    var elem = document.getElementById("your_elem_id");
    elem.style.display = '';    
</script>

Comments

0

With JQuery you can use $("#ElementID").css("display", ""); to clear the display attribute.

If it needs to be something else, just change the second parameter in the css() function.

More info here: http://api.jquery.com/css/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.