AnswerBun.com

Issue with event listener and value not updating

Stack Overflow Asked by penone on January 1, 2022

I am having an issue with an event listener on a select and a value not updating but cannot figure out what the issue.

Code is below. Basically need the price field to update based on the function above it. Unfortunately i cannot have them all in the same function and need to have them separate. The first console.log does show the value for y but the second does not show the value for price.

var factors = function factor(j) {
  var mySelect = document.getElementById("mySelect").value;
  if (mySelect == "car") {
    lease = "166";
  } else if (mySelect == "truck") {
    lease = "200";
  }
  console.log("this is my lease value - " + lease);
  return lease * j;
};

var monthlyprice = function monthlyprice() {
  var price = factors(1);
  console.log("this is my price -" + price);
};
var type = document.getElementById("mySelect");
type.addEventListener("click", factors);
<form action="">
  <label for="mySelect">Shipment Type</label>
  <select id="mySelect">
    <option value="car">Car</option>
    <option value="truck">Truck</option>
  </select>
</form>

One Answer

since monthlyprice calls factor() you want to place the listener on monthlyprice

var type = document.getElementById("mySelect");
type.addEventListener("change", monthlyprice);

function monthlyprice() {

  
    var price = factor(1)
 
  
  console.log("this is my price =" + price);
 
};

function factor(j) {
  var y;
  var z = document.getElementById("mySelect").value;
  if (z == "car") {
    y = "166";
  } else if (z == "truck") {
    y = "200";
  }
  console.log("this is my y value - " + y);
 
  return y * j;
  
};
<form action="">
  <label id="label">Shipment Type</label>
  <select id="mySelect">
    <option value="car">Car</option>
    <option value="truck">Truck</option>
  </select>
</form>

Answered by DCR on January 1, 2022

Add your own answers!

Related Questions

How do I keep a prompt at the bottom of the screen in C?

0  Asked on January 22, 2021 by wafwoof

 

URL query parameters to dict python

8  Asked on January 21, 2021 by leonardo-andrade

       

jQuery removeClass not working with draggable

1  Asked on January 21, 2021 by heila-al-mogren

     

How I can pass data between screens in React Native

2  Asked on January 21, 2021 by tuan-nguyen

   

Building a reference to a named column out of a passed vector

0  Asked on January 21, 2021 by sherman-sims

 

Splitting list into 2 parts, as equal to sum as possible

3  Asked on January 21, 2021 by jonathon

     

Camel router Builder with Java 6

0  Asked on January 21, 2021 by fiddle

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir