Insert To: DIV

Discussion in 'JavaScript' started by Linkbait, Jun 1, 2007.

  1. #1
    Please bear with me.... My PHP is great but when it comes to javascript I suck horibly.


    I am working with some sample code thats part of an ajax form. It uses the following to insert text back into the main page:
    function resultElement () {
    	if (!document.getElementById('result')) {
    		result = document.createElement('div');
    		result.id = 'result';
    		document.body.appendChild(result);
    	}
    }
    Code (markup):
    How would I modify this to insert where I want to on the main page? Right now it inserts back to the main page at the bottom no matter what I do...

    Help is greatly needed.
    Thanks.
     
    Linkbait, Jun 1, 2007 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you use:

    
    var nBody = document.body.getElementsByTagName('*');
    
    Code (markup):
    it will return an array of all the tags in the body.

    You can then use .insertBefore, for example, above the 3rd tag:

    
    var nBody = document.body.getElementsByTagName('*');
    var nDiv = document.createElement('div');
    document.body.insertBefore(nDiv,nBody[2])
    
    Code (markup):
    or to insert above the form tag
    
    var nFloor = document.body.getElementsByTagName('form')[0];
    var nDiv = document.createElement('div');
    document.body.insertBefore(nDiv,nFloor);
    
    Code (markup):
    I didn't test this, but that is the approach.
     
    Mike H., Jun 1, 2007 IP
  3. Linkbait

    Linkbait Peon

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, ill try it out and let ya know :)
     
    Linkbait, Jun 1, 2007 IP
  4. Linkbait

    Linkbait Peon

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, I could not get it to work with the code you posted so I went over the the MSDN center and did some reading for some well needed basic javascript skills.

    I ended up just using one line:

    
    result = document.getElementById('oIn');
    
    Code (markup):
    And then wrote another function to change the innerHTML:

    
    function handleResults () {
    	if (xml.readyState == 4) {
    		if (xml.responseText == 'ins.blank') {
    			result.innerHTML = 'Please Enter A Value!';
    		} else {
    			result.innerHTML = xml.responseText;
    		}
    	} else {
    		result.innerHTML = 'Working Please Wait';
    	}
    }
    
    Code (markup):
    Good thing I can learn quick :)
     
    Linkbait, Jun 1, 2007 IP