jquery - Converting inline Javascript to an onclick event -


i'll blunt. of skill in pure html , css. i'm trying more , more expand javascript , jquery , have experience, enough understand many simple tutorials , implement them own changes, not enough on own without cheatsheet or similar example work from. anyway:

i wanted try this tutorial found use ajax replace contents of div, implementation requires poor markup (inline js). doesn't suggest way utilize onclick event instead, , prefer inline js.

this "ajax engine" provides, choose import/link because feel it's silly dump in 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 open connection 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 use function requestcontent, offers option plop inline js so:

<ul> <li> <a href="javascript:requestcontent('/library/ajaxcontent1.html','fill-me3')"> click here</a> fill div below master form .php logo. </li> <li> <a href="javascript:requestcontent('/library/ajaxcontent2.html','fill-me3')"> click here</a> fill div below master form v4 logo. </li> <li> <a href="javascript:requestcontent('/library/ajaxcontent3.html','fill-me3')"> click here</a> fill div below popular logo. </li> </ul> 

i understand how inline js works, i'm not sure how write in way allow onclick event.

how go converting inline js? appreciate help.

using data attribute hold value allow add more links without having write more javascript.

html

<ul>   <li>     <a href="#" class="somelink" data-request-content="ajaxcontent1"> click here</a> fill div below master form .php logo.   </li>   <li>     <a href="#" class="somelink" data-request-content="ajaxcontent2"> click here</a> fill div below master form v4 logo.   </li>   <li>     <a href="#" class="somelink" data-request-content="ajaxcontent3"> click here</a> fill div below popular logo.   </li> </ul> 

javascript

$('.somelink').on('click', function(e) {   var content = $(e.currenttarget).data('request-content');   requestcontent('/library/' + content + '.html', 'fill-me3'); }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -