www.webdeveloper.com
Results 1 to 7 of 7

Thread: Inserting text into a textarea

  1. #1
    Join Date
    Dec 2011
    Posts
    163

    Inserting text into a textarea

    I currently have this HTMl code...

    HTML Code:
    <p><a class="insertCode" href="#">&lt;p&gt;&lt;/p&gt;</a> | <a class="insertCode" href="#">&lt;a target=&quot;&quot; href=&quot;&quot;&gt;&lt;/a&gt;</a> | <a class="insertCode" href="#">&lt;h1&gt;&lt;/h1&gt;</a></p>
    and this javascript...

    HTML Code:
    <script language="javascript" type="text/javascript">
    	$('a.insertCode').click(function()
    		{ 
       var htmltag = $(this).text();
       $('#codeArea').val($('#codeArea').val()+htmltag); 
       return false;
    		})
    </script>
    I am trying to make those links into "buttons" that will insert code into my textarea. So far I have nothing happeneing, even the links still work...

  2. #2
    Join Date
    Dec 2011
    Posts
    163
    No help?

  3. #3
    Join Date
    Nov 2010
    Posts
    1,083
    I notice you don't have your script wrapped in the customary document ready function - in this case you need to make sure that your html is coded before your script...

    Code:
    <body> 
    <p><a class="insertCode" href="#">&lt;p&gt;&lt;/p&gt;</a> | <a class="insertCode" href="#">&lt;a target=&quot;&quot; href=&quot;&quot;&gt;&lt;/a&gt;</a> | <a class="insertCode" href="#">&lt;h1&gt;&lt;/h1&gt;</a></p>
     <textarea id="codeArea"></textarea>
    <script type="text/javascript">
    	$('a.insertCode').click(function()
    		{ 
       var htmltag = $(this).text();
       $('#codeArea').val($('#codeArea').val()+htmltag); 
    		return false;   
    		})
    </script>
     </body>

  4. #4
    Join Date
    Dec 2011
    Posts
    163
    Yeah, this is the way I have it, but no dice.

    Even tried this...
    HTML Code:
    <script type="text/javascript">
    		$(function(){
    			$('a.insertCode').bind("click",function()
    			{ 
       			var htmltag = $(this).text();
       			$('#codeArea').val($('#codeArea').val()+htmltag);  
       			return false;
    			});
    		});
    		</script>
    Last edited by Dragonfire2008; 05-07-2012 at 10:28 PM.

  5. #5
    Join Date
    Nov 2010
    Posts
    1,083
    the code I posted works for me. must be something else.

  6. #6
    Join Date
    Dec 2011
    Posts
    163
    Ok I got it to work, for some reason the textarea has to come after the "links", otherwise it won't work...

    That's ok, I can work with it, thanks
    Last edited by Dragonfire2008; 05-07-2012 at 10:41 PM.

  7. #7
    Join Date
    Nov 2010
    Posts
    1,083

    this works for me in IE, FF and Chrome...

    Code:
    <body> 
    <textarea id="codeArea"></textarea>
    <p><a class="insertCode" href="#">&lt;p&gt;&lt;/p&gt;</a> | <a class="insertCode" href="#">&lt;a target=&quot;&quot; href=&quot;&quot;&gt;&lt;/a&gt;</a> | <a class="insertCode" href="#">&lt;h1&gt;&lt;/h1&gt;</a></p>
     
    <script type="text/javascript">
    	$('a.insertCode').click(function()
    		{ 
       var htmltag = $(this).text();
       $('#codeArea').val($('#codeArea').val()+htmltag); 
    		return false;   
    		})
    </script>
     </body>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles