SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Dom tree help

  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dom tree help

    I've got a bit of ajax code that loads some content into a div. What I want to do is alter the href and action of all links and forms loaded into the div.

    I thought the cleanest way to do this would have been to recursively look at all the child nodes of the div, and alter the href/action of any link or form elements that I encounter, is this do-able? I'm a complete noob when it come's to this dhtml dom stuff!

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you are using innerHTML to populate the div. If so, you can use getElementsByTagName on the parent element to get all the anchor and form elements.

    Code:
            function ajaxCallBack(result) {
                var parent = document.getElementById('someelement');
                parent.innerHTML = result.responseText;
                var anchors = parent.getElementsByTagName('A');
                var forms = parent.getElementsByTagName('FORM');
                
                // changes all urls in someelement to http://www.google.co.uk
                for(var i = 0; i < anchors.length; i++) {
                    anchors[i].href = 'http://www.google.co.uk/'; 
                }
                // changes all form actions in someelement to http://www.sitepoint.com/forums/
                for(var i = 0; i < forms.length; i++) {
    
                    // I haven't tested this code, so below may work fine. If not uncomment
                    // the line below and comment the line below that.
    
                    // forms[i].action = 'http://www.sitepoint.com/forums/';
                    forms[i].setAttribute('action', 'http://www.sitepoint.com/forums/');
                }
            }


  3. #3
    SitePoint Member
    Join Date
    May 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thats done the trick, cheers buddy!


Bookmarks

Posting Permissions

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