Get value from id and pass in input field

I’m trying to get the id from an image by clicking on it to store the id name as value in an input field.

Well, this works:

$(function(){

$(".image").click(function(){
    var key = $(this).attr("title");
   $("#description").val(key);
})

});

2 thoughts on “Get value from id and pass in input field”

  1. see my variant:

    <img src='myimg.jpg' id='my_id'>
    <button onclick='paste_id()'>click me</button>
    <input type='text' id='my_input'>
    
    <script>
    function paste_id(){
        let id_text = document.getElementById("my_id").getAttribute('id');
        let input = document.getElementById("my_input");
        
        input.value = id_text;
    }
    </script>
    
    Reply

Leave a Comment