Question

Is there any way to allow a link/anchor within an input field so that whatever text is in the field is ALSO clickable and actionable?

Was it helpful?

Solution

This is unfortunately not possible in HTML 4 or below. Even with HTML5 which has several new INPUT TYPEs, including URL, it only does validation and has some other useful functions, but won't give you want you want.

You might look for some jQuery plugins that can help you do this, most use the same principals behind Rich Text or other online/web-based HTML WYSIWYG editors. I've had trouble locating them myself.

These 3 situations (that I can think of right now) are pretty much what you will face natively with HTML4 or below, as text in an actual HTML4 INPUT textbox is pure text. It is not html and therefore NOT clickable. Here are some variations:

  1. The INPUT tag's VALUE attribute, also referenced as the corresponding DOM object's "value" property (which is basically what you've been doing, and the most you can hope for, if you decide that you MUST have the text that's ACTUALLY inside the textbox (because the text inside the textbox is the VALUE attribute, as I have it with "http://yahoo.com" in this example):

        <input id="myTxtbox" type="text" value="http://yahoo.com">
    

    where the INPUT's VALUE = "http://yahoo.com", which you can retrieve with:

    • in pure javascript:

      document.getElementById("myTxtbox").value

    • in jQuery:

      $("myTxtBox").val()

  2. When your link/url is the text in between the <INPUT> and </INPUT>, i.e. the text/innerText of the textbox. This is useless for your question/scenario since it's not clickable, and more importantly NOT INSIDE the textbox. However, someone might want to use this to retrieve any text that you may be using as a label (if you're not using the <label> tag itself already that is):

    <input id="myTxtbox" type="text">
      http://yahoo.com
    </input>
    

    The textbox's text/innerText is NOT an attribute here, only a DOM object property, but can still be retrieved:

    • pure javascript:

      document.getElementById("myTxtbox").innerText

    • jQuery:

      $("myTxtBox").text() -- you would use this to capure any text that you may be using as a label (if you're not using the tag).

    The result being: http://yahoo.com

  3. When your link/url is the form of an ANCHOR (<A>) with an HREF to your url (and visible link text) in between the <INPUT> and </INPUT>, i.e. the innerHTML of the textbox. This is getting a bit closer to what you want, as the link will appear as, and function as an actual link. However, it will NOT be inside of the textbox. It will be along side it as in example #2. Again, as stated in example #1, you CANNOT have actual working HTML, and therefore a working 'link' inside of a textbox:

    <input id="myTxtbox" type="text">
      <a href="http://yahoo.com">
        http://yahoo.com
      </a>
    </input>
    

    Once again, similarly to example #2, the textbox's innerHTML is NOT an attribute here, only a DOM object property, but can still be retrieved:

    • pure javascript:

      document.getElementById("myTxtbox").innerHTML

    • jQuery:

      $("myTxtBox").html()

    The result being: <a href="http://yahoo.com">http://yahoo.com</a>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top