Conflict in the use of Javascript functions

Asked

Viewed 45 times

0

A clear() function was created to clear the numbers inserted in a display by another addNumber() function, but the first function is not having any effect when used.

Tips on how I can solve this problem, or improve my code?.

const operation = document.querySelectorAll("[data-operation]")
const numbers = document.querySelectorAll("[data-content]")
const del = document.querySelector("[data-del]")
const allClear = document.querySelector("[data-ac]")
const displayPrevious = document.querySelector("[data-previous]")
const displayCurrent = document.querySelector("[data-current]")
const displayOperator = document.querySelector("[data-operator]")

function clear (){
    displayCurrent.innerHTML = " "
}
function addNumber(number){
    number = event.target.innerHTML
    if(number === "." && displayCurrent.innerHTML.includes(".")){
        return
    }
    displayCurrent.innerHTML += number
}
  • You have the HTML for this JS. I ask because depending on the elements evolved sometimes has one or two particularities that can be explored facilitating coding.

2 answers

0

Diego, since you didn’t post all the code, I imagined how it could be and I think it’s working properly:

const operation = document.querySelectorAll("[data-operation]");
const numbers = document.querySelectorAll("[data-content]");
const del = document.querySelector("[data-del]");
const allClear = document.querySelector("[data-ac]");
const displayPrevious = document.querySelector("[data-previous]");
const displayCurrent = document.querySelector("[data-current]");
const displayOperator = document.querySelector("[data-operator]");

function clear() {
    displayCurrent.innerHTML = " ";
}

function addNumber() {
    const number = event.target.innerHTML;
    if (number === "." && displayCurrent.innerHTML.includes(".")) {
        return;
    }

    displayCurrent.innerHTML += number;
}

numbers.forEach(a => a.addEventListener('click', addNumber));

allClear.addEventListener('click', clear);
div[data-content] {
  cursor: pointer;
  background: #ccc;
  padding: 6px;
  margin-bottom: 4px;
}

div[data-content]:hover {
  background: #efefef;
}
<div data-content>1</div>
<div data-content>2</div>
<div data-content>3</div>
<div data-content>.</div>
<div>
    data-current: <span data-current></span>
</div>
<button data-ac>clear</button>

If not, please post more details.

  • It really worked with this code of yours! the problem was that my click was being referred to in HTML and not with an addeventlistener in javascript?

  • @Diegosousa how good it worked! I don’t know exactly where your problem was because the code you posted lacked snippets of HTML and addEventListener but maybe that’s right. abs

-1

Its function has an inadequate space

**function clear (){**
  displayCurrent.innerHTML = " "
}

must be:

function clear(){
   displayCurrent.innerHTML = " "
}
  • Space was apparently not the problem. addNumber() works normally, but there is something that does not allow the clear() function to change the HTML created by addNumber()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.