简体   繁体   中英

html attribute value to be passed as an argument for a javascript function

I have the following html input attribute which I would like to pass its value as an argument to the function placeBid()

<input type = "number" name = "bidAmount" placeholder = "Bid Amount" required >

and this is where I would like to pass it:

<input type = "submit"  name = "submit" value = "PLACE BID" onclick="placeBid(bidAmount.value)">

any help on what I should put in the parameter?

You need to define an id for the input. and pass the id through onclick, like:

<input type = "number" name = "bidAmount" id= "bid-amount" placeholder = "Bid Amount" required >

<button type="button" onclick="placeBid('bid-amount')">Submit</button>

and get the value with javascript inside your function:

<script>
function placeBid(id){
    var value = document.getElementById(id).value;
    ...
}
</script>

Use an event listener instead.

EventTarget.addEventListener()

<input type = "number" name = "bidAmount" id="bidAmount" placeholder = "Bid Amount" required >

<input type = "submit"  name = "submit" id="placeBidBtn" value = "PLACE BID">
const bidAmount = document.getElementById("bidAmount");
const placeBidBtn = document.getElementById("placeBidBtn");

const placeBid = () => {
  console.log(bidAmount.value);
  // Place bid code here.
};

placeBidBtn.addEventListener("click", placeBid, false);

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