I'll be blunt. Most of my skill is in pure HTML and CSS. I'm trying more and more to expand into JavaScript and JQuery and have some experience, enough to understand many simple tutorials and implement them with my own changes, but not enough to do much on my own without a cheatsheet or a similar example to work from. Anyway:
I wanted to try THIS tutorial I found to use Ajax to replace the contents of a div, but the implementation requires poor markup (inline JS). He doesn't suggest a way to utilize an onclick event instead, and I would much prefer that to inline JS.
This is "Ajax Engine" he provides, which I choose to import/link because I feel it's silly to dump all that in the HTML:
<script type="text/javascript">
// Version 1.1 of May 16, 2013
function RequestContent(url,id) {
var http;
if (window.XMLHttpRequest) {
try { http = new XMLHttpRequest(); }
catch(e) {}
}
else if (window.ActiveXObject) {
try { http = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e) {
try { http = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) {}
}
}
if(! http) {
alert('\n\nSorry, unable to open a connection to the server.');
return false;
}
http.onreadystatechange = function() { PublishContent(http,id); };
http.open('GET',url,true);
http.send('');
}
function PublishContent(content,id) {
try {
if (content.readyState == 4) {
if(content.status == 200) { document.getElementById(id).innerHTML=content.responseText; }
else { alert('\n\nContent request error, status code:\n'+content.status+' '+content.statusText); }
}
}
catch(error) { alert('Error: '+error.name+' -- '+error.message); }
}
</script>
In order to use the function RequestContent, he only offers the option to plop inline JS like so:
<ul>
<li>
<a href="javascript:RequestContent('/library/ajaxcontent1.html','fill-me3')">
Click here</a> to fill the div below with the Master Form .PHP logo.
</li>
<li>
<a href="javascript:RequestContent('/library/ajaxcontent2.html','fill-me3')">
Click here</a> to fill the div below with the Master Form V4 logo.
</li>
<li>
<a href="javascript:RequestContent('/library/ajaxcontent3.html','fill-me3')">
Click here</a> to fill the div below with the Most Popular logo.
</li>
</ul>
I understand how the inline JS works, I'm just not sure how to write it in a way to allow for an onclick event.
How would I go about converting the inline JS? I really appreciate the help.