user3292394 user3292394 - 1 month ago 23
Ajax Question

Ajax in a wordpress page

I am trying to use the following piece of ajax jquery in a WordPress page:

1. <script>
2. $(document).ready(function(){
3. $("button").click(function(){
4. $.ajax({
5. method: 'GET',
6. url: "http:Sample.url.com",}).done(function (data)
7. {
8. var f_table= "";
9. for (var i = 0; i < data.unittopics.topics.length; i++) {
10. f_table+= "<tr><td>" +data.unittopics.topics[i].name+ "</td><td>" +data.unittopics.topics[i].description+ "</td><td>" + "<a id=" +'Frac_Link'+i+ " href='#'>" +'Click here'+ "</td></tr>";
11. alert("#div"+data.unittopics.topics[i].id);
}
12. $("#Frac_Table").append(f_table);
13. for (var x = 0; x < data.unittopics.topics.length; x++)
{
14. $("#Frac_Link"+x).attr('href', data.unittopics.topics[x].externalUrl);
alert("working");
}

});
});
});
</script>


I have tested this a jquery in w3school testing environment and it work perfect, but when I put it into WordPress it throws the following error
Invalid or unexpected token
on row 10. is there a simple way to fix this?

Thanks.

Answer

All the above are correct and solved another problem I didn't know I had. But the problem I posted about was resolved my separating the <tr><td> tags in my jQuery.

solution

jQuery(document).ready(function(){
jQuery("#frac_btn").one( "click", function() {
    jQuery.ajax({
        method: 'GET',
        url: "http:Sample.url.com",}).done(function (data)
            {
              var f_table= "";
              for (var i = 0; i < data.unittopics.topics.length; i++) {
              f_table+= "<tr"+"><td"+">" +data.unittopics.topics[i].name+  "</td"+"><td"+">" +data.unittopics.topics[i].description+ "</td"+"><td"+">" + "<a id=" +'Frac_Link'+i+ " href='#'>" +'Click here'+  "</td"+"></tr"+">";
            }
jQuery("#Frac_Table").append(f_table);
for (var x = 0; x < data.unittopics.topics.length; x++) 
 {
 jQuery("#Frac_Link"+x).attr('href', data.unittopics.topics[x].externalUrl);
 }
});
});
});
</script>
Comments