-1

I want get userID value for each form separately using javascript api.

I tried :

     <form >
     <input  list="browsers" id="${childSnapshot.val().orderid}" >
     <datalist id="browsers">
       <option value="Order Pending">
       <option value="Order Confirmed">
       <option value="Order Shipped">
       <option value="Order Delivered">
     </datalist>
   </form>
   <input id="${childSnapshot.val().orderid}" value="${childSnapshot.val().userID}"
   <button id="${childSnapshot.val().orderid}"  onclick="reply_click(this.id)" style="padding:1px">Update</button>
       

function reply_click(clicked_id){
  let valueitem = document.getElementById(clicked_id).value;
  var orderid = clicked_id;
  console.log(orderid+valueitem)
}

The result i'm getting Order id and the option value, but userID its not coming how to fix it ?

Ali Alsaggaf
  • 81
  • 10

2 Answers2

1

Try the following demonstrative snippet:

const button = document.getElementById('button');

const onClickHandler = (e) => {
  const orderID = e.target.dataset.orderId;
  const userID = document.getElementById(orderID).value;
  console.log(userID + ' ' + orderID);
}

button.addEventListener('click', onClickHandler);
<form>
  <input id="list" list="browsers">
  
  <datalist id="browsers">
    <option value="Order Pending">
    <option value="Order Confirmed">
    <option value="Order Shipped">
    <option value="Order Delivered">
  </datalist>
  
  <input id="${childSnapshot.val().orderid}" value="${childSnapshot.val().userID}"></input>

  <button id="button" type="button" style="padding: 1px" data-order-id="${childSnapshot.val().orderid}">Update</button>
</form>
Ernesto Stifano
  • 3,027
  • 1
  • 10
  • 20
0
    <input id="${childSnapshot.val().orderid}" value="${childSnapshot.val().userID}"
   <button id="${childSnapshot.val().orderid}"  onclick="reply_click(this.id)" style="padding:1px">Update</button>

You can't have two same id in html

Did you try this?

<button onclick="reply_click('${childSnapshot.val().orderid}')" style="padding:1px">Update</button>
Natsu
  • 150
  • 11
  • i tried this i got the orderid but userid value no – Ali Alsaggaf Aug 04 '21 at 10:22
  • You must remove the id from top too in your first input "id="${childSnapshot.val().orderid}"" because you using 3 same id in your html code. And at end of the second input you missing "/>" to close the tag. – Natsu Aug 04 '21 at 10:27