By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Update Input Field Based on Image Click
JavaScript

Javascript – Update Input Field Based on Image Click

Admin
Last updated: 2024/02/10 at 2:04 PM
Admin
Share
2 Min Read
Update Input Field Based on Image Click

Problem:

I am trying to update an input field based off what image the user clicks on. At the moment, I have images with the caption of a first and last name. When the user clicks on the image the first and last name is inserted into an input field (the picture is also highlighted but this is just using CSS to give the impression it’s selected).

Contents
Problem:Solution:

The user can select several images which insert multiple names into the input field. However I want to remove a name if the image becomes unselected.

$('#userImage').click(function(e) {
    $(e.target).toggleClass('active');

    var name = $(e.target).parent().find('figcaption').text().toLowerCase();

    if($(e.target).hasClass('active')) {
        if($('.username').val() == 0) {
            $('.username').val(name);
        } else {
            if($('.username').val().indexOf(name) == -1) {
                $('.username').val($('.username').val() + "," + name);
            }
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="userImage">
  <figure class="image">
      <img src="imgsrc">
      <figcaption>Bob Smith</figcaption>
  </figure>

  <figure class="image">
      <img src="imgsrc">
      <figcaption>John Doe</figcaption>
  </figure>
</div>

<input class="username" name="username">

The code checks to make sure the the image is selected first, if the box is empty it will insert the name, if it already has a value, it then checks to make sure the value isn’t repeated and inserts another name.

How could I remove just the name of the image that isn’t selected?

Solution:

I rewrote your code to the following:

let users = [];

$('#userImage').click(function(e) {
    let user = $(e.target);
    let input = $('.username');
    let name = user.parent().find('figcaption').text().toLowerCase();
    
    user.toggleClass('active');
        
    if(user.hasClass('active')) {
        users.push(name);
    } else {
      if(users.includes(name)) {
        users.splice(users.indexOf(name), 1);
      }
    }
    input.val(users.join(', '));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="userImage">
  <figure class="image">
      <img src="imgsrc">
      <figcaption>Bob Smith</figcaption>
  </figure>

  <figure class="image">
      <img src="imgsrc">
      <figcaption>John Doe</figcaption>
  </figure>
</div>

<input class="username" name="username">

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How can I get Websocket error code in javascript Javascript – How can I get Websocket error code in javascript
Next Article React firebase error: where' is not defined query call firebase in react Javascript – React firebase error: where’ is not defined query call firebase in react
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?