简体   繁体   中英

javascript if else statement not working. It returns singleStory even if I click on the twoStory image. I have tried != and ---

const detailedEstimate = () => {
document.querySelector("#btnDisplayImage").onclick = () => {


let houseStoriesLabel = document.createElement("label");
houseStoriesLabel.innerHTML = "<br>" + "number of stories is " + houseStory + "<br>";
document.body.appendChild(houseStoriesLabel);


if (houseStory = singleStory) {
  houseStory.setAttribute("src", "../images/singleStoryHouse.jpg");
  document.body.appendChild(houseStory);
  console.log('please make a choice');
}

You need:

if (houseStory === singleStory)

not

if (houseStory = singleStory)

The single equals sign assigns the value of singleStory to houseStory . === is what does comparison (or the much-frowned-upon == ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM