www.webdeveloper.com
Results 1 to 9 of 9

Thread: I need help with selecting a DOM node

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    Exclamation I need help with selecting a DOM node

    HTML Code:
    <a class="lbcontainer" onclick="linkbuttonajax('http://example.com',1,true);return false;"><span class="lbbutton"></span><span class="lbcount"><button type="button" class="lbnum" name="num">1</button></span></a>
    linkbuttonajax() changes the value of the button. However, I'm having trouble selecting the button. I tried the following, and many other methods:

    this.getElementsByTagName('button').innerText
    this.lastchild.num.innerText
    this.num.childNodes[1].childNodes[0].innerText

    However, none of these work.

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    this.getElementsByTagName('button')[0].firstChild.data
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Mar 2010
    Posts
    701
    I'm getting this error: Uncaught TypeError: Object [object DOMWindow] has no method 'getElementsByTagName'

    The page with the button is here: http://linksku.com
    The buttons are the blue ones near the center of the page.

  4. #4
    Join Date
    Mar 2010
    Posts
    701
    I need urgent help with this. My entire site doesn't work without this.

  5. #5
    Join Date
    Dec 2009
    Location
    Shropshire UK
    Posts
    153
    Where is the linkbuttonajax function? Difficult to give an answer without seeing it.

    The problem is that 'this' isn't in the context of the object firing the event. Instead it's in the context of window.

    Try a simple window.getElementById('whatever') and you'll get the same error.

  6. #6
    Join Date
    Mar 2010
    Posts
    701
    I got it working by adding a new parameter to linkbuttonajax(). So now it's like onclick="linkbuttonajax('http://example.com',1,true,this.childNodes[1].childNodes[0]);return false;"

    Why does "this" work in the onclick, but not in the function?

  7. #7
    Join Date
    Mar 2009
    Posts
    495
    I'm getting this error: Uncaught TypeError: Object [object DOMWindow] has no method 'getElementsByTagName'

    try window.document.getElementsByTagName() instead.

  8. #8
    Join Date
    Dec 2009
    Location
    Shropshire UK
    Posts
    153
    Why does "this" work in the onclick, but not in the function?
    That's impossible to say without looking at the function. You usually get this issue with events and Internet Explorer as it wrongly binds this to window. There are work arounds for that though.

    In your case however I'm guessing it's related to Ajax, which unfortunately I'm not that clued up on.

    That said if you are at liberty to post the code for linkbuttonajax, I'm sure someone else here could shed some light on it.

    Good you've got it working though.

    I got it working by adding a new parameter to linkbuttonajax(). So now it's like onclick="linkbuttonajax('http://example.com',1,true,this.childNodes[1].childNodes[0]);return false;"
    That extra parameter sets the context I'm guessing.

    Somewhere in there I'm guessing it's using a call or apply, which that parameter is passed into.

    Simple example.

    Code:
    var obj = {name:'Fred'};
    
    function sayName(message){ alert (message + this.name) }
    
    // call sayName in the context of obj
    sayName.call(obj, 'Hello '); // Hello Fred
    Last edited by rpg2009; 12-05-2010 at 12:46 PM.

  9. #9
    Join Date
    Mar 2010
    Posts
    701
    Code:
    function linkbuttonajax(url,votes,submitted,button) {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReq.open('POST', '../wp-includes/submit.php', true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
                updatelinkbutton(button,self.xmlHttpReq.responseText);
            }
        }
        url=encodeURIComponent(url);
        self.xmlHttpReq.send('v='+votes+'&k=ci3HPCL6gp&url='+url);
    if(submitted==false)
    window.open('http://linksku.com/submit?url='+url,'submit','width=550,height=550');
    }
    
    function updatelinkbutton(num,str){
    if((parseFloat(str) == parseInt(str)) && !isNaN(str))
    num.innerText = str;
    else if(str=='login')
    window.open('http://linksku.com/login','login','width=550,height=550');
    }
    The buttons that call this function is everyone on http://linksku
    I want to create a site similar to Digg, where users can "vote" on the best links.

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