user2062819
user2062819

Reputation: 3

javascript value from input

<script type="text/javascript">
        function changeText2(){
            var userInput = document.getElementById('userInput').value;
            var lnk = document.getElementById('lnk');
            lnk.href = "https://www.facebook.com/sharer/sharer.php?u=http://facebook.com/" + userInput;
            lnk.innerHTML = lnk.href;
        }
        </script>


    <a href="" id=lnk></a> <br>
    <input type='text' id='userInput' value='' /> <br>
    <input type='button' onclick='changeText2()' value='Go'/>

I found that code on some website , i want to display an img not the url... Im new in javascript

Upvotes: 0

Views: 196

Answers (1)

David Starkey
David Starkey

Reputation: 1840

Something like this should work:

<a href="" id="fbLink" style="display:none;">
  <img src="YOUR_IMAGE_HERE.jpg"/>
</a>
<input type='text' id='userInput' value='' onchange="shLink()"/>
<script>
  function shLink(){
    if(document.getElementById("userInput").value!=""){
      if(document.getElementById("fbLink").style.display=="none"){
        document.getElementById("fbLink").style.display="";
        document.getElementById("fbLink").href="https://www.facebook.com/sharer/sharer.php?u=http://facebook.com/"+document.getElementById("userInput").value;
      }
    }else{
      document.getElementById("fbLink").style.display="";
      document.getElementById("fbLink").href="";
    }
  }
<script>

The script will toggle the display of the link if there is a value entered into your input. It will be called when the input value is changed (onchange). Alternatively, you could use onkeypress to call the function whenever the user types something in your input.

Upvotes: 1

Related Questions