最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if div contains certain text or not? - Stack Overflow

programmeradmin0浏览0评论

I added some text to div like this:

document.getElementById("" + choice).innerHTML += "e";

But later in my script I want to check if that div contains my text. I have tried the below code:

if (document.getElementById("" + choice).innerHTML === "e") {
    alert('yep!');
}

But it doesn't work, I don't get the "yep!' alert. How do I fix it?

I added some text to div like this:

document.getElementById("" + choice).innerHTML += "e";

But later in my script I want to check if that div contains my text. I have tried the below code:

if (document.getElementById("" + choice).innerHTML === "e") {
    alert('yep!');
}

But it doesn't work, I don't get the "yep!' alert. How do I fix it?

Share Improve this question edited Sep 21, 2018 at 10:20 Vinit Desai 5201 gold badge4 silver badges20 bronze badges asked Sep 21, 2018 at 9:13 sovel2sovel2 4154 silver badges12 bronze badges 4
  • 1 What was it's innerHTML before? – Luca Kiebel Commented Sep 21, 2018 at 9:15
  • You are using innerHTML += "e";thus adding something to the HTML – enxaneta Commented Sep 21, 2018 at 9:16
  • += "e" implies content was already there, whereas you’re testing for === ‘e’, exactly-equal to ‘e’. – David Thomas Commented Sep 21, 2018 at 9:17
  • @LucaKiebel it was empty – sovel2 Commented Sep 21, 2018 at 9:34
Add a ment  | 

6 Answers 6

Reset to default 4

You are appending the text using += operator and not pletely setting it to e.

Thus, if the innerHTML was foo it would be fooe.

What you might want to do is:

if ((document.getElementById("" + choice).innerHTML).indexOf("e") !== -1) {
    alert('yep!');
}

You should use :

if (document.getElementById("hello").innerHTML.includes("e")) {
    alert('yep!');
}
<div id="hello">hello world</div>

Because in this code:

document.getElementById("" + choice).innerHTML += "e";

you are adding "e" to innerHTML

use document.getElementById("" + choice).innerHTML.contains("e")

You can do it this way:

var myContent = "e"

document.getElementById("" + choice).innerHTML += myContent;

if (document.getElementById("" + choice).innerHTML.includes(myContent)) {
    alert('yep!');
}

If you did intend to use the += operator instead of =. You might have whitespace in your div. You can use the trim function to get rid of that like in this snippet.

If you change the operator to = you can still use the check without trim.

document.getElementById("test").innerHTML += "e";
console.log('innerHTML value: "' + document.getElementById("test").innerHTML + '"');
if (document.getElementById("test").innerHTML === "e") {
    console.log('passed');
}
if (document.getElementById("test").innerHTML.trim() === "e") {
    console.log('passed with trim');
}
<div id="test"> </div>

+= means concat new value to current value. i guess if you want to replace the div's innerHTML to "e", youshould replace "+=" in

document.getElementById("" + choice).innerHTML += "e";

to "=". else if you'd like to append string to div's innerHTML, you should replace '===' in

if (document.getElementById("" + choice).innerHTML === "e") {
    alert('yep!');
}

to includes function.

发布评论

评论列表(0)

  1. 暂无评论