Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ziaul sarker
3,652 PointsjavaScript if els
<!DOCTYPE html> <html> <body>
<h1>JavaScript in Body</h1>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
<script> function myFunction() {
var x = document.getElementById("demo").innerHTML;
if (x = "A Paragraph.") {
x.innerHTML = "Paragrapg changed."
}
else {
x.innerHTML = "A Paragraph."
}
} </script>
</body> </html>
2 Answers
Chris Adamson
132,143 PointsIt looks like:
x = "A Paragraph."
should be
x == "A Paragraph."
since you probably want to do a comparison, not assignment.
Daniel Newman
Courses Plus Student 10,715 Pointsfunction myFunction() {
var x = document.getElementById("demo").innerHTML;
if (x == "A Paragraph.") {
x.innerHTML = "Paragrapg changed.";
} else {
x.innerHTML = "A Paragraph.";
}
}
The problem here is:
x = document.getElementById("demo").innerHTML;
x.innerHTML = "Paragrapg changed.";
x.innerHTML = "A Paragraph.";
It's looks like:
document.getElementById("demo").innerHTML.innerHTML = "Some text";
Try to keep x as a link to the object, not the text
function myFunction() {
var x = document.getElementById("demo");
if (x.innerHTML == "A Paragraph.") {
x.innerHTML = "Paragrapg changed.";
} else {
x.innerHTML = "A Paragraph.";
}
}
ziaul sarker
3,652 Pointsthank you so much brother... much appreciated. makes sense now. you are the man thanks again
ziaul sarker
3,652 Pointsziaul sarker
3,652 Pointsi am trying that but it doesn't seem to work