javascript - Compare .innerHTML with a string - Stack Overflow

I have a playpause button and I want to check whether its text is "Play" or "Pause"

I have a play/pause button and I want to check whether its text is "Play" or "Pause" when the user clicks on it.

window.PlayPauseButton = document.getElementById("PlayPauseButton");

PlayPauseButton.onclick = function()  //when you click the play or pause button
{
    if( window.PlayPauseButton.innerHTML == "Pause" )
    {
        window.PlayPauseButton.innerHTML = "Play";
        clearInterval( window.TheTimer );
    }
    else
    {
        window.PlayPauseButton.innerHTML = "Pause";
    }
};

But it doesn't work! I can't pare innerHTML with a string.

I have a play/pause button and I want to check whether its text is "Play" or "Pause" when the user clicks on it.

window.PlayPauseButton = document.getElementById("PlayPauseButton");

PlayPauseButton.onclick = function()  //when you click the play or pause button
{
    if( window.PlayPauseButton.innerHTML == "Pause" )
    {
        window.PlayPauseButton.innerHTML = "Play";
        clearInterval( window.TheTimer );
    }
    else
    {
        window.PlayPauseButton.innerHTML = "Pause";
    }
};

But it doesn't work! I can't pare innerHTML with a string.

Share edited Feb 7, 2022 at 14:13 Adil Hussain 32.3k23 gold badges125 silver badges166 bronze badges asked Jun 2, 2016 at 23:45 BenBen 1031 gold badge3 silver badges8 bronze badges 1
  • innerHTML is a string value and pares to strings quite nicely. However, you have not included the HTML nor the error message, so all someone can do is guess. "It doesn't work" is the worst description of a bug -- you should always say exactly WHAT isn't happening. – Jeremy J Starcher Commented Jun 2, 2016 at 23:56
Add a ment  | 

2 Answers 2

Reset to default 4

You need to check with innerText, not innerHTML

PlayPauseButton.onclick = function()  //when you click the play or pause button
{
    if( window.PlayPauseButton.innerText== "Pause" )
    {
        window.PlayPauseButton.innerText= "Play";
        clearInterval( window.TheTimer );
    }
    else
    {
        window.PlayPauseButton.innerText= "Pause";
    }
};

If you're using a button tag, try using innerText instead:

if (window.PlayPauseButton.innerText == "Pause" ){
    window.PlayPauseButton.innerHTML = "Play";
    clearInterval( window.TheTimer );
}
else  {
    window.PlayPauseButton.innerHTML = "Pause";
}

This will only work if you're using <button>Play</button>.

If you're using an <input type="button"> you're going to need to pare the value instead of innerText.

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

相关推荐

  • javascript - Compare .innerHTML with a string - Stack Overflow

    I have a playpause button and I want to check whether its text is "Play" or "Pause"

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信