skip to Main Content

This form value is submitted at the number of times a click on the user is performed.
What I want to do is submit the value to the clicked user, not to the number of times I clicked on a user or various users.

You can run the code here, to see what exactly I’m talking about, you will see the result at the console.

Thanks

https://jsfiddle.net/sambello/75uysd4c/53/

<form>
 <p><strong>USER LIST</strong></p>
 <div class="form-group boxed">
   <div class="input-wrapper">
     <input type="text" id="message" name="message" class="form-control" placeholder="Type a message...">
     <i class="clear-input">
       <ion-icon name="close-circle"></ion-icon>
     </i>
   </div>
</div>
  <button type="button" id="submit" class="btn btn-icon btn-primary rounded">
    <ion-icon name="send">Send</ion-icon>
  </button>
  
</form>
let users = document.querySelectorAll('.users');
users.forEach(user => {
    user.addEventListener('click', function() {
     
     let query_id = $(this).data("id");
     let query_username = $(this).data("username");
   
    
     let btn_submit = document.querySelector('#submit');
            btn_submit.addEventListener('click', function() {
                        
            let chat_message = document.getElementById("message").value;
            
            //This value below will display/submit at the number of time you click on the users.
            console.log(query_id);
            console.log(query_username);
            console.log(chat_message)
                
                        // Ajax Goes Below
            
                });
  
  });
    
});

2

Answers


  1. You are adding a listener to the button, each time you click the user, you should only add one eventListener to the button, I have attached a working snippet.

    let users = document.querySelectorAll('.users');
    let query_id = null;
    let query_username = null;
    
    users.forEach(user => {
      user.addEventListener('click', function() {
        query_id = $(this).data("id");
        query_username = $(this).data("username");
      });
    });
    
    let btn_submit = document.querySelector('#submit');
    btn_submit.addEventListener('click', function() {
      let chat_message = document.getElementById("message").value;
      console.log(query_id);
      console.log(query_username);
      console.log(chat_message);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <p>
      <strong>USER LIST</strong>
    </p>
    <table>
      <tbody>
        <tr>
          <td>
            <div class="users" data-id="1" data-username="sam"> SAM </div>
          </td>
        </tr>
        <tr>
          <td>
            <div class="users" data-id="2" data-username="Tj"> TJ </div>
          </td>
        </tr>
        <tr>
          <td>
            <div class="users" data-id="3" data-username="elite"> Elite </div>
          </td>
        </tr>
      </tbody>
    </table>
    <form>
      <div class="form-group boxed">
        <div class="input-wrapper">
          <input type="text" id="message" name="message" class="form-control" placeholder="Type a message...">
          <i class="clear-input">
            <ion-icon name="close-circle"></ion-icon>
          </i>
        </div>
      </div>
      <button type="button" id="submit" class="btn btn-icon btn-primary rounded">
        <ion-icon name="send">Send</ion-icon>
      </button>
    </form>
    Login or Signup to reply.
  2. Sounds like you need a radio button for each user. So, you can select only one of the users if you give all the <input> the same name attribute.

    If you don’t like the look of the radio button you can hide it using CSS.

    I added a e.preventDefault(); to stop the form from submitting here on ST. You can just remove that if you need the form to do a POST request to your backend.

    document.forms.form01.addEventListener('submit', e => {
      e.preventDefault();
      let form = e.target;
      let user = form.querySelector(`input[name="user"][value="${form.user.value}"]`);
      console.log('Say', form.message.value, 'to', user.dataset.username, form.user.value);
      // print out the data from the form
      let formData = new FormData(form);
      console.log([...formData.entries()].map(el => `${el[0]}: ${el[1]}`).join(';'));
    });
    label {
      display: block;
    }
    
    input[type="radio"] {
      display: none;
    }
    
    input[type="radio"]:checked~span {
      background-color: orange;
    }
    <form name="form01">
      <p><strong>USER LIST</strong></p>
      <label><input type="radio" name="user" value="1" data-username="SAM"/><span>SAM</span></label>
      <label><input type="radio" name="user" value="2" data-username="TJ"/><span>TJ</span></label>
      <label><input type="radio" name="user" value="3" data-username="Elite"/><span>Elite</span></label>
      <label><input type="text" name="message" class="form-control" placeholder="Type a message..."></label>
      <button class="btn btn-icon btn-primary rounded">
        <ion-icon name="send">Send</ion-icon>
      </button>
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search