LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2022, 12:07 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,177

Rep: Reputation: 165Reputation: 165
javascript trouble comparing string with innerHTML


I have the following javascript function:
Code:
function chxMe(f)
{
    var t = document.getElementById("disable_" + f).innerHTML;
    alert(t);

    if (t == '<img src="/images/minus-circle.png">')
    {
        alert("Blah, blah, blah");
    }
 
}
The alert(t) shows exactly: <img src="/images/minus-circle.png">, and I've added a screen-shot image of the alert box, just in case I'm typing what I want to see.

This code post is a bit confusing. I've had to add a leading / to the src= because otherwise this editor inserts "https://www.linuxquestions.org/questions" in my src= value. Refer to the image for the exact string.

Why does the 'if' never return true? I've also tried === as the comparator.

Click image for larger version

Name:	innerHTML.jpg
Views:	15
Size:	5.0 KB
ID:	39382

Last edited by mfoley; 08-12-2022 at 12:11 PM.
 
Old 08-12-2022, 12:39 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,795

Rep: Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049
Quote:
Originally Posted by mfoley View Post
Why does the 'if' never return true?
Because the values being compared do not match.

You've not provided enough context for anyone else to confirm why they do not match, but whitespace is a significant possibility.


Also, what is "this editor", because the one I'm typing in does NOT change the src of <img src="images/minus-circle.png"> nor of
Code:
<img src="images/minus-circle.png">
 
1 members found this post helpful.
Old 08-12-2022, 12:41 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,353
Blog Entries: 1

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
For example, it might contain whitespace characters. Convert the string to hexadecimal before printing it.
 
Old 08-12-2022, 04:10 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,636

Rep: Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024
Don't try to compare the innerHTML. Just don't. If you thought you had to do it that way, find another way to do it.
 
2 members found this post helpful.
  


Reply

Tags
innerhtml, javascript


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
problem in comparing numeric with string naren_0101bits Programming 1 01-28-2008 08:10 AM
[php] comparing found username with string Erhnam Programming 4 03-15-2005 09:34 PM
storing and comparing a string Randall Programming 18 03-10-2003 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration