2

I have a div with a string inside of it. I want to change that string if something specific happens. I am not sure why but I can't get the string to change at all even if there is no conditional. I am very rusty to JavaScript so any advice would be appreciated, I am slowly getting back to the basics.

<div class="cell text-center" id="changeString"> Report has not yet been pulled for this organization.</div>

var bananas = true;      
if (bananas = true){
    document.getElementById('changeString').innerHTML = "You have successfully run the report.";
}

Can anyone see what I am doing wrong? I have also tried putting the JS into a function and calling it in the div with onload="methodName".

7
  • 5
    Missing quotes in getElementById(changeString) and the = in the if should be == or ===. = is assignment, == and === are comparison Commented Oct 5, 2016 at 20:12
  • changeString is a variable that refers to the DOM element. getElementById however expects to be passed a string value. So either do changeString.innerHTML = ... or document.getElementById('changeString').innerHTML = ... Commented Oct 5, 2016 at 20:15
  • Good catch thank you. var bananas = true; console.log(bananas); if (bananas == true){ document.getElementById("changeString").innerHTML = "You have successfully run the report."; } This still doesn't work but I can see in the console that it is returning true. Commented Oct 5, 2016 at 20:19
  • Make sure you run the JavaScript after the div was loaded. It should work. See jsfiddle.net/jkdmzvo4 Commented Oct 5, 2016 at 20:24
  • if you run in it in html add script tags jsfiddle.net/maio/8qt9fzjr/2 Commented Oct 5, 2016 at 20:25

1 Answer 1

3

Just like @j08691 mentioned in the above comment you should add quotes to getElementById() :

document.getElementById("changeString").innerHTML = "You have successfully run the report."
________________________^____________^

And to use == or === in your condition for comparison instead of equal = that used for assignment:

if (bananas === true){

And it will work as you could see below.

Hope this helps.

document.addEventListener("DOMContentLoaded", function(event) { 
  var bananas = true;

  if (bananas === true){
    document.getElementById("changeString").innerHTML = "You have successfully run the report."
  }
});
<div class="cell text-center" id="changeString"> Report has not yet been pulled for this organization.</div>

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your feedback. I believe I am running into an issue with my IDE, your code and the JSFiddle provided all work, just not with my current settings for some reason.
You're welcome, have you tried to add code inside ready function? try my update..
That strange have you tried to add an alert or console.log() to make sure that this part of your code is reached?
yes, and it is logging in the console. It is so strange...my only last guess is that it is because I am writing HTML in a Visualforce page and they don't like each other. I already have about 800 lines that have no errors so I am just as confused as you are!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.