Sir
Sir

Reputation: 8280

Problems with calling a JS function

I have a function which populates a div but includes js onclick. How ever its producing some weird error i've not seen before.

This is the function:

//Browser Support Code for AJAX Requests
function ajaxFunction(url,data){
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
  querystring = "?dta="+data;
     AJAX.open("GET", url + querystring, false);                             
     AJAX.send(null);
     return AJAX.responseText;                                         
  } else {
     return false;
  }                                             
}       


function subpopulate(id,array,type){ alert('test2'); // alert to check if called
document.getElementById(id).innerHTML += "<div class='subm'>test</div>";
}

function menu(type,scri,get){ alert('test'); //alert to check if called
        var result = ajaxFunction(scri,get);
        subpopulate('offset',result,type);
}

function loadmain(id){
document.getElementById(id).innerHTML = "<table style='width:100%;'><tr><td><a href='#' onclick='javascript:menu('build','build.php','false');'>Build</a></td></tr></table>";
}

When i click the link it doesn't call "menu" function it errors with this:

Uncaught SyntaxError: Unexpected token }

In console errors all I see is for its information is this below:

 window.script1332821403739=1;

Any ideas why this is happening :S ?

Upvotes: 0

Views: 86

Answers (3)

pylover
pylover

Reputation: 8055

Change

onclick='javascript:menu('build','build.php','false');'

to

onclick='javascript:menu(\'build\',\'build.php\',\'false\');'

Upvotes: 1

Epuri
Epuri

Reputation: 300

Replace onclick event code in loadmain method as following:

onclick='javascript:menu(\"build\",\"build.php\",\"false\");'

Upvotes: 1

Peter Lyons
Peter Lyons

Reputation: 145994

You need to escape the single quotes within your javascript code you have embedded in your HTML table tag.

Upvotes: 1

Related Questions