0

So I have a variable in my script var mystring = "123" and my web page look like this

<body>  
<div id="footer">   
123  
</div> </body>

I need a script that checks if the variable is the same as the footer content and if is not, replace it.

Just like this :

a="123";  
if a == html.content.from.footer then  
   do nothing;   
else   
   replace html.content.from.footer with a;
2
  • 2
    Have you tried writing anything? Commented Jun 29, 2013 at 18:07
  • 3
    Your English is fine. Commented Jun 29, 2013 at 18:07

4 Answers 4

5

There's no need to check if the values are equal, because if you replace a value with an equal value then there's no change anyway. So all you really need to do is replace the value. Something like this:

document.getElementById('footer').innerHTML = mystring;
Sign up to request clarification or add additional context in comments.

Comments

4

Using jQuery, this is simple:

if($("#footer").html() !== mystring) {
    $("#footer").html(mystring)
}

Without jQuery:

if(document.getElementById('footer').innerHTML !== mystring) {
    document.getElementById('footer').innerHTML = mystring;
}

3 Comments

this question is not related to jquery and jquery doesn't bring anything 'special' here
But if you're already using jQuery (as many projects are), it's easier to do it that way.
Ya, maybe you are right. +1 because your answer bring 2 possibilities which is always good to know
0

Here,

<script>
 var mystring = "123";
 var footer = document.getElementById("footer");
 if(footer.innerHTML != mystring){
   footer.innderHTML = mystring;
 }
</script>

PS. I agree with David that you don't need to check if you know the string you want in the footer anyway. Just adding the script with the if block because you asked it that way. Hope this helps.

Comments

0
var myVar = '1234';
var element = document.getElementById('footer');

if(myVar != element.innerHTML) {
    element.innerHTML = myVar;
}

Comments

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.