Display certain image for derminated numbers

Asked

Viewed 39 times

-1

I got the following <input> and <img> on the page.

<input class="form-control" id="cordenadasaqui" value="47644.63956 74592.843" readonly>

<img id="imagemaqui" src="http://www.meusite.com/los-santos.jpg">

I need to display a different image for each set of displayed strings.

Example


If the <input> above is with the value=" " containing in the first 11 characters:

47644.63956

the image displayed will be:

<img id="imagemaqui" src="http://www.meusite.com/los-santos.jpg">


If you have the value=" " containing the following numbers in the first 11 characters:

87635.98456

the image displayed will be:

<img id="imagemaqui" src="http://www.meusite.com/las-venturas.jpg">


To simplify the question

In the value=" " a set of numbers will be loaded, and then only the first 11 characters (10 numbers and 1 point ".").

If these first 11 characters are 47644.63956 = los-santos.jpg

If these first 11 characters are 87635.98456 = las-venturas.jpg

And so on... How to do this?

  • 1

    values will already be filled in the input? or when the user type the image will appear?

  • They will already be filled in the input friend :D

  • 1

    intends to use js?

  • I think it would be an alternative. :)

  • 1

    I’ll make a code here and already answer your question...

  • Thank you friend! D

Show 1 more comment

1 answer

2


I believe this code solves your problem. By loading the page it takes the value of the input and compares according to what Oce said. And assigning the image:

<input class="form-control" id="cordenadasaqui" value="87635.98456 74592.843" readonly>

<img id="imagemaqui" src="http://www.meusite.com/los-santos.jpg">
<script>
$(document).ready(function(){
   var coordenadas= $("#cordenadasaqui").val().substr(0,11);
   var img="";
if(coordenadas=="47644.63956"){
img="http://www.meusite.com/los-santos.jpg";
    }
else if(coordenadas=="87635.98456"){
        img="http://www.meusite.com/las-venturas.jpg";
    }
    $("#imagemaqui").attr("src",img);
});
</script>

I used Jquery. I hope I helped!!

  • I’m going to test it now! D

  • It was perfect!!! Thank you very much, my congratulations for your knowledge and for helping the laity here at Sopt! : ) kkkkk...

  • 1

    You’re welcome to help :)

  • Friend, could you give an additional example of instead of assigning an image, assigning a text? Instead of a <img> one <font> ?

  • 1

    Put another question, because I didn’t understand it well. But I think I would follow the same logic, only instead of $("#imagemaqui").attr("src",img); would be something like that $("font").html("seu texto");

  • Vlww!!! I had to change to: $("#imagemaqui").text(img);

  • 1

    I’m glad you made it :)

Show 2 more comments

Browser other questions tagged

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