How to get the value of a tag and send it to another tag with js

1

Hello, guys, I have a problem that is as follows.

I have 4 tags, but I want to send the value of the tag that was clicked to the input value.

Example:

<img id="imgs" src="img_01">
<img id="imgs" src="img_02">
<img id="imgs" src="img_03">
<img id="imgs" src="img_04">
<input type="text" nome="selecionar_img" value="">

Suppose I click on the img of src 3, I need my input this way.

<input type="text" nome="selecionar_img" value="img_03">

And if I click on another tag img the value field delete the previous value and put the new value of the tag that was clicked.

    <input type="text" nome="selecionar_img" value="img_03"> // Valor anterior
    <input type="text" nome="selecionar_img" value="img_01"> // Novo Valor passado

Thanks to anyone who can help me, I have no experience with Js but I need to do these events. obg ^ - ^

    
asked by anonymous 30.12.2017 / 16:28

1 answer

0

First of all, two remarks: All your img tags have the same value in the id attribute. According to the html specification, the value of this element must be unique . These values should be in the class attribute. In addition, the attribute name in input is name and not nome .

So your tags would look like this:

<img class="imgs" src="img_01">
<img class="imgs" src="img_02">
<img class="imgs" src="img_03">
<img class="imgs" src="img_04">
<input type="text" name="selecionar_img" value="">

To fill the input with the src attribute of the image, you can do this:

$(document).ready(function(){
    $(".imgs").click(function(){
        $("input[name=selecionar_img]").val($(this).attr('src'));
    });
});
    
30.12.2017 / 16:51