admin管理员组文章数量:1431406
I have the following in my project:
function check() {
var empt = document.getElementById('xyz1').value;
if (empt == "") {
alert("Please input a Value");
return false;
} else {
return true;
}
}
<a href="test.html" role="button" name="weiter" id="weiter" onclick="check();">Go</a>
<p id="xyz1"></p>
I have the following in my project:
function check() {
var empt = document.getElementById('xyz1').value;
if (empt == "") {
alert("Please input a Value");
return false;
} else {
return true;
}
}
<a href="test.html" role="button" name="weiter" id="weiter" onclick="check();">Go</a>
<p id="xyz1"></p>
Something I am doing wrong. I always get to the page test.html. The check is not working. Is it even possible, to check if a p tag is empty?
Goal is, if there is nothing in the p tag to get an alert and if there is a string or number getting to the linked page test.html
I would like to work in pure JavaScript (no jQuery).
-
p
doesn't have avalue
property. UsetextContent
orinnerText
to check if the element contains any text, to check if it contains any child HTML theninnerHTML
will suffice. – Deepak Kamat Commented Dec 31, 2017 at 14:03 - 1 use innerHTML function – madhur Commented Dec 31, 2017 at 14:03
-
@madhur
innerHTML
isn’t a function. – Sebastian Simon Commented Dec 31, 2017 at 17:47 - I mean innerHTML property on element – madhur Commented Dec 31, 2017 at 19:18
2 Answers
Reset to default 3It's innerHTML instead of value. Also, check for null.
function check() {
var empt = document.getElementById('xyz1').innerHTML;
if (empt == null || empt == "")
{
alert("Please input a Value");
return false;
}
else
{
return true;
}
}
<a href="test.html" role="button" name="weiter" id="weiter" onclick="check();">Go</a>
<p id="xyz1"></p>
function check() {
event.preventDefault();
var empt = document.getElementById('xyz1').innerHTML;
if (empt == null || empt == "")
{
alert("Please input a Value");
return false;
}
else
{
return true;
}
}
<a href="test.html" role="button" name="weiter" id="weiter" onclick="check();">Go</a>
<p id="xyz1"></p>
Function must be event prevent default otherwise run href after on click event.
Another Method
onclick="return check()"
function check() {
var empt = document.getElementById('xyz1').innerHTML;
if (empt == null || empt == "")
{
alert("Please input a Value");
return false;
}
else
{
return true;
}
}
<a href="test.html" role="button" name="weiter" id="weiter" onclick="return check();">Go</a>
<p id="xyz1"></p>
本文标签: JavaScript check if html p tag is emptyStack Overflow
版权声明:本文标题:JavaScript: check if html p tag is empty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745570135a2663997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论