How to load an image into an img input type input by receiving the url of another input

Asked

Viewed 756 times

1

Good night.

I have a form that has an input that receives a url by ajax.

How can I take this url and fill in another input to view the image ?

JS (here I pass the id of the input that will receive the url):

$("#thumb").val(json.items[0].volumeInfo.imageLinks.thumbnail);

INPUT 1 (here the input is filled with the url I will save):

<input type="text" id="thumb" value="http://books.google.com/books/content?id=-2DSc30QWpEC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api"  />                                              

INPUT 2 (Here I would like to show the image and I can’t):

<input type="image" src="" id="thumb2" >    

It is possible?

  • I do not understand very well what you want to do. I could explain better?

  • no input type="img"... would not be input type="image"?

  • @lffg type in the first input I get the URL to save in my database. This input will not appear to the user. In the second input I wanted to show the image .

  • @dvd already fixed. Sorry.

1 answer

1


You have to insert the image in src of input #thumb2 catching the value of input #thumb, after the return of Ajax on :success:

<script>
...ajax
success: function() {
    $("#thumb2").attr("src",$("#thumb").val());
}
</script>

Inputs of the type image need to have a src where they will pull an image, which will be the content of the element.

Further information in this MDN documentation.

  • I made the modifications you passed and still do not fill. I put this suggestion $("#thumb2"). attr("src",$("#Thumb").val()); and it didn’t work.

  • @Amandarj put $("#thumb2").attr("src",$("#thumb").val()); only after the return of Ajax.

  • @Tamdarj That’s when the input thumb is already filled value.

  • @Amandarj, if you are receiving an image via AJAX, you must pass a parameter to the function success, thus: success: function(linkImage) { and replace $("#thumb").val() for linkImage. If you are using an object/array, you need to put the full path, e.g.: linkImage.thumbnail.src.

  • @dvd, had already done what you said. I could only make it work using this: <img src="""id="thumb2" alt=""> . Thank you, working perfectly.

  • @Amandarj cool. Dei +1 in your question.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.