0
<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

5
  • 2
    Can you show the code you have so far? Commented Apr 2, 2013 at 20:36
  • Do you know any line of JavaScript? Do you know how to access DOM elements? Do you know how to get attributes of those elements? Do you know how to set the visibility of elements? If you can answer this questions with yes you should be able to do it. Commented Apr 2, 2013 at 20:38
  • 1
    @user2062819: Never ever post your code in a comment. Edit your question instead. Commented Apr 2, 2013 at 20:39
  • sorry ! , i have edit my question now :) Commented Apr 2, 2013 at 20:41
  • That code works exactly like you want it to. Proof here, I made no edits, just copy and paste: jsfiddle.net/77SPz/1 What's the problem? You want to display an image? An image inside the link? Commented Apr 2, 2013 at 20:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

That doesnt change the share link. if a user write " hello " i want the share url to be facebook.com/sharer/sharer.php?u=http://facebook.com/hello
Aha sorry hah! hmm , i dont get it to work :( ,it only displays " - " and its not clickable
yea i have dont that and my img displays but its not have any link to facebookshare?
Now it doesnt display my img at all "/ haha
It's been a long day apparently -_-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.