Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: createTextNode

  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    createTextNode

    Hello, I'm working on a textarea box and I'm wanting to make it so when a user clicks on a link a BB tags much like what you see here will appear right after all of the text you just typed in. So it would be the exact same as if you were typing on this forum and asking a question and you went to the top and clicked the Wrap [PHP] tag and out comes the tags right where your pointer is at. So I got this to work just fine on IE but oddly enough it isn't working the same on net scape browsers.

    http://idigteck.com/mess.php


    Try it out for yourself and see what I mean. Note you have to type in some text for it to mess up.

    I tried different ways of doing this and non work so far. What am I doing wrong here?


    Code:
    <script type="text/javascript">
    	function appendTag(eid) {
    	var txtArea = document.getElementById('txtArea');
    	var bbtags = new Array('', '');
    		
    		var aTag = document.createTextNode(bbtags[0]);
    		txtArea.appendChild(aTag);
    	}
    </script>
    Last edited by Jon W; 12-01-2009 at 11:44 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    <script type="text/javascript">
    	function appendTag(eid) {
    	var txtArea = document.getElementById('txtArea');
            if(txtArea){
             	var bbtags = new Array('', '');
    		
    		var aTag = document.createTextNode(bbtags[0]);
    		txtArea.appendChild(aTag);
            }else{
                    alert('no element with id txtArea');
            }
    	}
    </script>
    best regards

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Oesxyl, thanks for your reply. Unfortunately this doesn't fix the problem that I was having. I still have the exact same problems as before.

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    This is what something I was trying to do.

    Code:
    <script type="text/javascript">
    	function appendTag(eid) {
    	var txtArea = document.getElementById('txtArea');
            if(txtArea){
             	var bbtags = new Array('', '');
    		
    		var aTag = document.createTextNode(bbtags[0]);
    		var txt = txtArea.value;
    		txtArea.value = txt+bbtags[0];
            }else{
                    alert('no element with id txtArea');
            }
    	}
    </script>
    This is working the way that I wanted. Thanks for the help.

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Better yet. Here is the full finish script. Anyone can use this if they need a script like this.

    Code:
    <script type="text/javascript">
    function appendTag(tagName) {
    var txtArea = document.getElementById('txtArea');
    	if(txtArea) {
    	
    	if(!txt) var txt = txtArea.value; // Get the current text and store it inside of a variable
    	
    	if(!bbTags) var bbTags = new Array('','','[img][/img]','','');
    	
    	switch(tagName) {
    		case 'a':
    		txtArea.value = txt+bbTags[0];
    		break;
    		case 'u':
    		txtArea.value = txt+bbTags[1];
    		break;
    		case 'img':
    		txtArea.value = txt+bbTags[2];
    		break;
    		case 'b':
    		txtArea.value = txt+bbTags[3];
    		break;
    		case 'i':
    		txtArea.value = txt+bbTags[4];
    		break;
    	}
    		
    	}
    	else
    	{
    	alert('Oops I missed up.');
    	}
    }
    
    </script>
    Thanks!
    Last edited by Jon W; 12-01-2009 at 01:38 PM.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    <script type="text/javascript">
    function appendTag(tagName) {
    var txtArea = document.getElementById('txtArea');
    	if(txtArea && txtArea.value) {
               // if bbTags is not declared outside of function you don't need to test it
               // but if exist one you can use a local new version this way
    	   var bbTags = new Array('','','[img][/img]','','');
    	
    	switch(tagName) {
    		case 'a':
    		txtArea.value += bbTags[0];
    		break;
    		case 'u':
    		txtArea.value += bbTags[1];
    		break;
    		case 'img':
    		txtArea.value += bbTags[2];
    		break;
    		case 'b':
    		txtArea.value += bbTags[3];
    		break;
    		case 'i':
    		txtArea.value += bbTags[4];
    		break;
    	}
    		
    	}
    	else
    	{
    	alert('Oops I missed up.');
    	}
    }
    
    </script>
    also you can use a associative array and 'in' operator and get rid of switch.

    Code:
    var bbTags = {'url':'','u':'','img':'[img][/img]','b':'','i':''};
    if(tagName in bbTags){
      txtArray.value += bbTags[tagName];
    }
    best regards


  •  

    Posting Permissions

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