How to make something appear when a certain word is typed in a textbox?

Discussion in 'JavaScript' started by x0x, Mar 20, 2010.

  1. #1
    I have a textbox:

    <input type="text" size="40" name="profilemediaurl" />

    and I also have a checkbox with some text behind it:

    <input type="checkbox" name="autoplay" /> Autoplay?


    What I want to do is to make the checkbox and "Autoplay?" text appear when the user pasts or types a youtube link into the checkbox. I think "youtube.com" would be a good trigger - when the user types or pastes that the checkbox appears.

    Would it be possible to make?
     
    x0x, Mar 20, 2010 IP
  2. mattinblack

    mattinblack Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    perfectly easy to do
    Put a div with a known id where you want the checkbox etc to appear. Put &nbsp; inside the div.
    Have a javascript timer set to every 300ms or so that calls a function that examines the contents of the textbox for the characters.
    This function then sets the innerhtml property of the div to equal the html code you want to display, or to &nbsp; if youtube is not found and resets the checkbox value (this way it clears the checkbox if the link is deleted)
     
    mattinblack, Mar 20, 2010 IP
  3. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You could also work with the onchange or onkeyup/onkeydown events in the input textbox which will call your testing function
     
    Nyu, Mar 23, 2010 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Thanks. This is what I ended up doing:




    <script type="text/javascript">
    function div_appear(){

    var str=document.getElementById("mediaurl").value;
    var pos=str.indexOf("youtube");
    if (pos>=0)
    {
    document.getElementById("autoplay_div").style.display = "";
    }else{
    document.getElementById("autoplay_div").style.display = "none";
    }
    }
    </script>





    <input type="text" onkeyup="div_appear();" size="40" id="mediaurl" name="mediaurl" />




    <div style="display: none" id="autoplay_div">
    <input type="checkbox" name="autoplay" />
    Autoplay?
    </div>
     
    Last edited: Mar 24, 2010
    x0x, Mar 24, 2010 IP
  5. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Within the event in the input box change "row_appear" to "row_appear()"
    Then it works for me in Firefox ;)
     
    Nyu, Mar 24, 2010 IP
  6. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Yea, I found that out when I was reading my post. :)
     
    x0x, Mar 24, 2010 IP
  7. mattinblack

    mattinblack Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    And dont forget to deselect the checkbox when you hide it - if somebody types in a youtube address, clicks the checkbox then changes their mind and types in a normal address the form will still get sent the checbox value.
     
    mattinblack, Mar 24, 2010 IP