JavaScript change image on input key press

I have a web page with an input where users enter 8 characters.

I would like to be able .... after the FIRST character entered in an input tag

To "set" an image

The first character will always be numeric in a alphanumeric string

So...
In txtUserInput a user enters a "1"

The image would be set... and display with a src="../img/sub/1Href.jpg"

The user types In a "2"

The image would be set... and display with a src="../img/sub/2Href.jpg"

<input id="txtUserInput" runat="server" class="field" placeholder="Enter Code" style="border: solid 1px #c1c1c1; width: 70%; font-size: 50px; float: left;" />

<img src="#" alt="" />
Larry Bristersr. DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
HTML5
Look at the input element.I set a onkeypress function returning only nymbers and maxlenght value equal to ONE ie 1or 2 or 3....or 9.
<!DOCTYPE html>
<html>
<head>
<title>HTML, CSS and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->

<input id="myNum" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' maxlength="1"/>
<img id="myImg" src="#" alt="" />
  <script>
//Define and set str var
   var str="Href.jpg";
//Add keyup event at input element
document.getElementById('myNum').addEventListener('keyup',function(){
//Set the src value depending on num
   document.getElementById('myImg').setAttribute('src','../img/sub/'+this.value+str );
});
  </script>
<!-- End your code here -->
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leonidas DosasCommented:
Altrenative with JQUERY:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<title>HTML, CSS and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->

<input id="myNum" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' maxlength="1"/>
<img id="myImg" src="#" alt="" />
  <script>
  $('#myNum').on('keyup',function(){  
  $('#myImg').attr('src', '../img/sub/'+$(this).val()+'Href.jpg');
});
  </script>
<!-- End your code here -->
</body>
</html>

Open in new window

0
Larry Bristersr. DeveloperAuthor Commented:
Hey Leonidas
  It works on the character but there is a limit on characters

I need to keep typing.  The change is on the first character only.
0
Larry Bristersr. DeveloperAuthor Commented:
Thanks!   Got me on the right track.

I added this to set it to first character

this.value.charAt(0)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.