Home > Blockchain >  Multiple callbacks on JS event
Multiple callbacks on JS event

Time:10-09

I have a form with two inputs fields.

  <form action="file.js" method="post" >
    <div>
        <p>Year</p>
    </div>
    <div>
        <input type="number" name="Year"></input>
    </div>

    <div>
        <p>Name</p>
    </div>
    <div>
        <input type="text" name="Tvshow"></input>
    </div>
    <div>
       <button >Send</button>
    </div>
</form>

On the one hand Year field will obtain year chosen by user to show any tvshow and tvshow field will save tvshow name to show it base on moviedb Api request.

This is my javascript code:

const callbackYear = Year_User => 
{
    Year_User(Year);
}

const callbackTvshow = show => 
{
  show(name)
}

const getData = () => {
    const button = document.querySelector(".btn");
    button.addEventListener("click", () => {
        const Form = document.querySelector(".FormData");
        const Year = new FormData(Form).get("Year");
        const name = new FormData(Form).get("Tvshow");
        callbackYear(Year);
        callbackTvshow(name);
    });
}

callbackYear((Year_User) => 
{
  console.log(Year_User);
})

callbackTvshow((show) => 
{
  console.log(show);
})

Basically what I'm trying to do is to get both data (Year and Tvshow name) with two severals callbacks called Year_User and show to access them in another functions.

CodePudding user response:

seems that you put the wrong name to the Tv show property

The code:

    const Year = new FormData(Form).get("Year");
    const name = new FormData(Form).get("Name");
    alert(Year);
    alert(name);

Prints the "Year" and "null" cause "Name" is not a field name... The correct one would be "Tvshow"

  const Year = new FormData(Form).get("Year");
  const name = new FormData(Form).get("Tvshow");
  alert(Year);
  alert(name);

It outputs the Year and the name correctly.

CodePudding user response:

The above code is calling getData twice with one param.

But getData accepts two params at the same time:

getData(
  (Year_User) => { console.log(Year_User) }, 
  (Tvshow) => { console.log(Tvshow) }
)
  • Related