javascript - Show div hidden by "display: none" using JS? - Stack Overflow

The below HTML and JavaScript is working as expected, but I want to make sure I am using this correctly

The below HTML and JavaScript is working as expected, but I want to make sure I am using this correctly.

I have two divs but only one needs to be displayed depending on the value of mode.

HTML:

<body>
        <div id="a-div" style="display: none;">
            <button id="add" class="btnstyle">Add </button>
        </div>
        <div id="d-div" style="display: none;">
            <button id="delete" class="btnstyle">Delete</button>
        </div>
</body>

JS:

//$("#a-div").hide();
//$("#d-div").hide();

var mode = 'add';
//var mode = 'delete';
if (mode === 'add') {
    $("#a-div").show();
} else {
    $("#d-div").show();
}

This is giving me expected results. Is there a better way of reversing the style="display: none" attribute?

The below HTML and JavaScript is working as expected, but I want to make sure I am using this correctly.

I have two divs but only one needs to be displayed depending on the value of mode.

HTML:

<body>
        <div id="a-div" style="display: none;">
            <button id="add" class="btnstyle">Add </button>
        </div>
        <div id="d-div" style="display: none;">
            <button id="delete" class="btnstyle">Delete</button>
        </div>
</body>

JS:

//$("#a-div").hide();
//$("#d-div").hide();

var mode = 'add';
//var mode = 'delete';
if (mode === 'add') {
    $("#a-div").show();
} else {
    $("#d-div").show();
}

This is giving me expected results. Is there a better way of reversing the style="display: none" attribute?

Share Improve this question edited Sep 16, 2015 at 19:55 Anders 8,65310 gold badges60 silver badges99 bronze badges asked Sep 16, 2015 at 19:35 NikNik 1711 gold badge3 silver badges13 bronze badges 3
  • 3 Why do you have “smart quotes” ‘’ in your code instead of straight quotes: ''? – Sebastian Simon Commented Sep 16, 2015 at 19:38
  • 2 You're definitely doing it correctly. show() is designed for jobs like this. – tcooc Commented Sep 16, 2015 at 19:46
  • @ Xufox I wrote this sample code in a word document (bad choice) – Nik Commented Sep 16, 2015 at 19:46
Add a ment  | 

5 Answers 5

Reset to default 1

Your current code should be working fine, but there are many ways of solving this problem. I would remend using jQuerys toggle():

$("#a-div").toggle(mode === "add");
$("#a-div").toggle(mode === "delete");

Alternatively, you could give them the id´s add-div and delete-div and make one of them visible like this:

$("#" + mode + "-div").show();

You can use:

$("#a-div").toggle();

Alternatively;
.show() / .hide()
.fadeIn() / fadeOut()

There's several methods to do this (like you can see in other answers) but i think the show() function is enough and do the work in your case.

You can also use css() method of JQuery like following :

$("#a-div").css('display','block');

Hope this helps.

You need to modify the display property of the inline style attribute.

Like this...

var mode = 'add';

if (mode === 'add') {
    $("#a-div")[0].style.display = 'block';
} else {
    $("#d-div")[0].style.display = 'block';
}

Or you can use something like inherit instead of block.

Another option is to move the styles to css:

html:

    <div id="a-div" class="notdisplayed">
        <button id="add" class="btnstyle">Add </button>
    </div>
    <div id="d-div" class="notdisplayed">
        <button id="delete" class="btnstyle">Delete</button>
    </div>

css:

.notDisplayed {display:none;}

Script:

$("#a-div").addClass("notDisplayed");
$("#d-div").removeClass("notDisplayed");

This method is more general than show/hide, as it can be extended to any style rule.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745195020a4616047.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信