www.webdeveloper.com
Results 1 to 2 of 2

Thread: bObj button onclick

  1. #1
    Join Date
    Nov 2009
    Posts
    268

    Question bObj button onclick

    Another Weerd One. I got an insert row function running with bObj but wanted to add some flexibility to the function and decided to add some if statements to it but can't figure out why it isn't working as exspected.

    SCRIPT
    Code:
    function addtoinvoice(){
    var bObj=document.getElementsByClassName('button');
    for(var i=0; i< bObj.length; i++)
    {
    if(bObj[i].innerText=='Buy More!') //added if to test button change?
    {
    bObj[i].onclick = function()
    {
    //alert('Yeah!');
    alerted();//should work!!
    return;//to avoid rest of script
    }
    if(bObj[i].innerText==='Buy Now!')
    {
    bObj[i].onclick = function()
    {
    //alert(bObj[0].innerText);
    additem(this);
    }
    }
    };
    }
     
    function additem(button)//oh that's it have to reference button??
    {
    alert(this.innerText);
    }
    //onclick doesn't run at all??
     
    function alerted(button)//refferenced button
    {
    alert(this.innerText);
    }
    //this should work?
    Any advice greatly appreciated.

  2. #2
    Join Date
    Nov 2009
    Posts
    268

    Question bObj button onclick

    This only runs the first function which is the important one, but I'd like to run the second alerted() function because the innerText of the buttons are changed and I don't wan't to change their classNames even though that maybe the way to go?
    SCRIPT
    Code:
    function addtoinvoice(){
    var bObj=document.getElementsByClassName('button');
    for(var i=0; i< bObj.length; i++)
    {
    if(bObj[i].innerText==='Buy Now!') //RUNS!!
    {
    bObj[i].onclick = function()
    {
    //alert(bObj[0].innerText);
    additem(this);
    return;
    }
    if(bObj[i].innerText==='Buy More!')//DOESN'T
    {
    bObj[i].onclick = function()
    {
    alerted();//OOPS left out (this)
    return;
    }
    }
    };
    }
     
    
    }
    Left out this in alerted()

    No! not even adding the this solved it. Anybody explain why?
    Last edited by THEFOOL; 04-26-2012 at 07:58 PM. Reason: No! not even adding the this solved it. Anybody explain why?

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