Help with FORM selection

Discussion in 'PHP' started by jrp1, May 8, 2009.

  1. #1
    Im working on a script where, when you click an image, it automatically enters the text "FOO BAR" into a form field. How can I make it so just "FOO" is highlighted when this text is entered? Here's my current code:

    <div>
    <img src="/images/file.png" name="image" onClick="sendText(document.search.q, 'FOO BAR')">
    </div>
    Code (markup):
     
    jrp1, May 8, 2009 IP
  2. jrp1

    jrp1 Active Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    To elaborate, I need both words to be inserted into the form via Javascript, but only the word "FOO" to be highlighted.
     
    jrp1, May 8, 2009 IP
  3. usadesi

    usadesi Peon

    Messages:
    296
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your information....
     
    usadesi, May 8, 2009 IP
  4. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This should work. Uses two methods - one for i.e. and another for firefox etc. Click the "Button" button to see it work.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=windows-1250">
      <meta name="generator" content="PSPad editor, www.pspad.com">
      <title>Yeehaw</title>
      <script type="text/javascript">
    function highlight_foo()
    {
    
    var first_word="foo";
    var second_word="bar";
    var word_length=first_word.length;
    myForm.myText.value=first_word+=" ";
    myForm.myText.value+=second_word;
    // firefox method
    try
      {
    myForm.myText.setSelectionRange(0,word_length);
    myForm.focus;
      }
    // i.e. method
    catch(err)
      {
    var oRange = myForm.myText.createTextRange();
    oRange.moveStart("character", 0);
    oRange.moveEnd("character", word_length - myForm.myText.value.length);
    oRange.select();
    oTextbox.focus();
      }
    
    }
    </script>
      </head>
      <body>
      <form name="myForm">
    Text: <input type="text" name="myText" />
    <input type="button" name="the_button" value="button" onClick="highlight_foo()" />
    </form>
    
      </body>
    </html>
    Code (markup):
     
    JDevereux, May 8, 2009 IP
  5. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This script is modified version of the script provided by JDevereux. Actually the script provided by the JDevereux works but it also throws javascript error. Well i just modify the script and now it's working and not throwing any exception or javascript error as well. I checked this script in Internet Explorer, Google Chrome and Firefox. Here's the script

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1250">

    <title>Highlighting text in Javascript</title>
    <script type="text/javascript" language="javascript">
    function highlightText()
    {
    var browserName=navigator.appName;
    document.getElementById("myText").value="FOO BAR";
    var text_length;
    text_length=document.getElementById("myText").value;
    if(browserName=="Netscape")
    {
    document.getElementById("myText").setSelectionRange(0,3);
    }
    else if(browserName=="Microsoft Internet Explorer")
    {
    var oRange = document.getElementById("myText").createTextRange();
    oRange.moveStart("character", 0);
    oRange.moveEnd("character", 3 - document.getElementById("myText").value.length);
    oRange.select();
    }
    else
    {
    }
    }
    </script>
    </head>
    <body>
    <form name="myForm">
    Text: <input type="text" name="myText" id="myText"/>
    <input type="button" name="the_button" value="button" onClick="highlightText();" />
    </form>

    </body>
    </html>
     
    mrmaf, May 9, 2009 IP