开发者

Hidden textarea validation

How can I ch开发者_开发知识库eck if textarea is hidden using javascript?


var textArea = document.getElementById('textAreaId');

if (textArea.currentStyle.visibility === 'hidden' || textArea.currentStyle.display === 'none')
{
   /* code */
}


var myBox = document.getElementById("myBox");
if (myBox.currentStyle.display === "none" || myBox.currentStyle.visibility === "hidden") {
  alert("Box is invisible");
}

-- Works with

<textarea id="myBox">Lorem ipsum doloet set amit</textarea>


Internet explorer somehow gets confused if you have two elements with same id. Though the things work fine in firefox, they don't in Internet explorer. I changed the id of textarea and it is working now.

Thank You guys.


Did you try elm.getBoundingClientRect() ?

It will give all zero values if the element or a parent has display:none.
With visibility:hidden the element is there and then has a boudning rectangle.

<html>
<head>
    <title>hidden</title>
</head>
<body>
<div style="display:none">
    <form>
        <textarea></textarea>
    </form>
</div>
<script>
  var rect = document.getElementsByTagName('TEXTAREA')[0].getBoundingClientRect();
  alert(rect.right === 0 ? 'hidden':'visible');
</script>
</body>
</html>


Wouldn't it be unhidden in the first place if your css isn't setting it to display: none; ?

If you want to hide it or show it you should just be able to use some JQuery:

$(document.body).css( "display", "none" );

or

$(myForm.elements).hide()

And so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜