[javascript] How to set "style=display:none;" using jQuery's attr method?

Following is the form with id msform that I want to apply style="display:none" attribute to.

<form id="msform" style="display:none;">
</form>

Also the check should be performed before adding the "style=display:none;" property. That is if it is already set like in above code it should not set again.

But if it's not set then it should.

How should I achieve this? Please help me.

This question is related to javascript jquery html css show-hide

The answer is


Please try below code for it :

$('#msform').fadeOut(50);

$('#msform').fadeIn(50);

You can use the hide and show functions of jquery. Examples

In your case just set $('#msform').hide() or $('#msform').show()


Based on the comment we are removing one property from style attribute.

Here this was not affect, but when more property are used within the style it helpful.

$('#msform').css('display', '')

After this we use

$("#msform").show();

You can use the jquery attr() method to achieve the setting of teh attribute and the method removeAttr() to delete the attribute for your element msform As seen in the code

$('#msform').attr('style', 'display:none;');


$('#msform').removeAttr('style');

$(document).ready(function(){
var display =  $("#msform").css("display");
    if(display!="none")
    {
        $("#msform").attr("style", "display:none");
    }
});

As an alternative to hide() mentioned in other answers, you can use css() to set the display value explicitly:

$("#msform").css("display","none")

You can just use: $("#msform").hide(). This sets the element to display: none


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to show-hide

How to set "style=display:none;" using jQuery's attr method? How to Toggle a div's visibility by using a button click How to hide iOS status bar Toggle visibility property of div android layout with visibility GONE Show pop-ups the most elegant way Show hide fragment in android Show/Hide Div on Scroll How to add display:inline-block in a jQuery show() function? Android: show/hide status bar/power bar