SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Can't Make Same Ajac call from two different Onclicks?

    I'm trying to call the same Ajax get PHP from two different elements Onclick. The second one isn't working.

    Index.js
    Code:
    function getproduct(p){
    if(p==' '){
    document.getElementById('f1').innerHTML=' ';
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else{// code for IE6, IE5
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById('f1').innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open('GET','getproduct.php?q='+p,true);
    xmlhttp.send();
    }//the function I'm trying to call from two different Onclicks
    
    var products=new Array();
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(i=0;i<sidebarli.length;i++){   
    if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
    sidebarli[i].onclick=function(){   
    EvalSound1();
    var p=this.id;
    alert(p+': Is sending your request.');
    products.push(p);
    getproduct(p);
    document.getElementById('logo').className=p;
    f1.className='product';
    document.getElementById('blank').src='scripts/product.js';
    hidewelcome();
    };
    }//function works
    
    var breturn=document.getElementById('breturn');
    addEvent(breturn,'click',function(){
    var p=document.getElementById('productreturn').value;
    alert('Getting product: '+p);
    getproduct(p);
    f1prompt.style.display='none';
    EvalSound2();
    },false);//function doesn't work????????
    Any help greatly appreciated

  2. #2
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb RESOLVED:Can't Make Same AjaX call from two different Onclicks?

    Sorry, I forgot I had nested the second Ajax Call within a Function that querried the className of div id 'f1' which was class 'htmlpage' and needed to be changed to class 'product' before it could run the second function like so
    Index.js
    Code:
    var breturn=document.getElementById('breturn');
    addEvent(breturn,'click',function(){
    f1.className='product'; //had to change div className back to appropriate class
    var p=document.getElementById('productreturn').value;
    alert('Getting product: '+p);
    getproduct(p);
    f1prompt.style.display='none';
    EvalSound2();
    },false);//Now works!!!
    Lesson for the day, If your going to call a function more than once make sure that you haven't set any conditions that block it!?????


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
  •