www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to insert this text node?

  1. #1
    Join Date
    Oct 2013
    Posts
    5

    How to insert this text node?

    Assume I have a HTML code similar to:

    <div class="aaa bbb ccc">
    <p class="text">....</p>
    <p class="text">....</p>
    ....
    <p class="text">....</p>
    </div>

    Now I want to add at the bottom of the <div> element another
    text (lets say: var = document.URL)

    How do I do this (with jQuery if pure JS not possible)?

    The following does not work:

    var adr = document.URL;
    var div = document.getElementByClass("aaa bbb ccc");
    div.appendChild(document.createTextNode(adr));

    Related, advanced question: How do I do this WITH enclosing <p class="text"> ....</p>
    as the previous blocks? So afterwards the web page should contain as if author would have coded

    <div class="aaa bbb ccc">
    <p class="text">....</p>
    <p class="text">....</p>
    ....
    <p class="text">....</p>
    <p class="text">http://www.thisisthedomain.com/foobar.html</p>
    </div>

    Peter

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>title</title>
    <style>
    body{text-align:center;color:#000;background-color:#fff;font-family:'Bookman Old Style',Georgia,Verdana;font-size:14px;}
    div{padding:5px;border:1px solid Crimson;margin-top:10px;background-color:#C9D7BE;}
    p.text{margin-top:5px;border:1px dashed #abc;color:#000;background-color:#FAF6D1;}
    input[type=button]{cursor:pointer;margin-top:10px;}
    </style>
    <script>
    window.onload=function(){
    document.getElementById('ins').onclick=function(){
    var div=document.getElementsByClassName('aaa bbb ccc')[0],p=document.createElement('p');
    p.className='text';
    p.innerHTML=document.URL;
    div.appendChild(p);
    document.getElementById('res').innerHTML='<h2>innerHTML:</h2>'+div.innerHTML.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\&lt;p/g,'<br />&lt;p');
    }
    }
    </script>
    </head>
    <body>
    <div class="aaa bbb ccc">
    <p class="text">....</p>
    <p class="text">....</p>
    <p class="text">....</p>
    </div>
    <div class="aaa bbb ccc">
    <p class="text">1111</p>
    <p class="text">2222</p>
    <p class="text">3333</p>
    </div>
    <input id="ins" type="button" value="Insert smth" />
    <div id="res"></div>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,221
    turning things in to a useful function...

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>title</title>
    <style>
    body{
    	text-align:center;color:#000;
    	background-color:#66CC99;
    	font-family:Verdana, Arial, Helvetica, sans-serif;
    	font-size:14px;
    	}
    div{
    	padding:5px;
    	border:1px solid Orange;
    	margin-top:10px;
    	background-color:#FFCC00;
    	}
    p.text{
    	margin-top:5px;
    	border:1px dashed #abc;
    	color:#000;
    	background-color:#99FF99;
    	}
    /* input[type=button]{cursor:pointer;margin-top:10px;} */
    </style>
    <script>
    Object.prototype.appendTarget = function(nodeType,nodeNumber,insertElement,insertData){
    	var div=this.getElementsByTagName(nodeType)[nodeNumber];
    	p=document.createElement(insertElement);
    	//p.className='text';
    	p.innerHTML=insertData;
    	div.appendChild(p);
    }
    <!-- When the page loads, the DIV order will change! -->
    window.onload=function(){
    	<!-- Add a new DIV ro DIV[0] -->
    	document.appendTarget('DIV',0,"div","");
    	<!-- this new div becomes DIV[1], add a P tag element to it -->
    	document.appendTarget('DIV',1,"p",document.URL);
    	<!-- the old DIV[1] is now DIV[2] -->
    	document.appendTarget('DIV',2,"p","TaDah!");
    }
    </script>
    </head>
    <body>
    <!-- This is DIV [0] -->
    	<div class="aaa bbb ccc">
    		<p class="text">.</p>
    		<p class="text">.</p>
    		<p class="text">.</p>
    	</div>
    <!-- This is DIV [1] -->	
    	<div class="aaa bbb ccc">
    		<p class="text">1111</p>
    		<p class="text">2222</p>
    		<p class="text">3333</p>
    	</div>
    
    </body>
    </html>
    Should be self explanatory
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    thumbs up ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

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