Highlighting textarea text in Javascript

Discussion in 'JavaScript' started by learnwebsitedesigncom, May 20, 2011.

  1. #1
    I am trying to understand to the code example at the top works and the code example at the bottom does not when the only difference is that the code example at the top is using the function name "select" and no matter what the name of the function of the bottom code example is, it does not work.

    Javascript code example #1
    
    function select()
     {
      document.getElementById(first).focus();
      document.getElementById(first).select();
     }
    
    Code (markup):
    Javascript code example #2
    
    function find()
     {
      document.getElementById(first).focus();
      document.getElementById(first).select();
     }
    
    Code (markup):
    HTML Code
    
    <form action='' method='post' name='ex_form'>
    
    <textarea rows='5' cols='30' id='first' onClick="select();">
    This is some text.
    </textarea>
    
    </form>
    
    Code (markup):

     
    learnwebsitedesigncom, May 20, 2011 IP
  2. Sefrez

    Sefrez Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    first should be enclosed in parenthesis. getElementById expects a string. Also, quick trick: :)
    
    <script type="text/javascript">
    function select(text)
    {
    	text.focus();
    	text.select();
    }
    </script>
    <form action='' method='post' name='ex_form'>
    	<textarea rows='5' cols='30' id='first' onClick="select(this)">This is some text.</textarea>
    </form>
    
    Code (markup):
     
    Sefrez, May 20, 2011 IP