jQuery .load(jQuery(this).attr("id")) vs .load("theID") - how to translate? -
jQuery .load(jQuery(this).attr("id")) vs .load("theID") - how to translate? -
i have list (menu). when user clicks on link, want load content ajax. article loaded determined based on links id attribute.
example:
<ul class="ajaxmenu"> <li><a id="services">services</a> ... </u>
the variables store links article, named same links' id (i.e. services , url ajax phone call stored on var services)
this works:
var services = "index.php?option=com_content&view=article&id=2&tmpl=component"; jquery(".ajaxmenu > li > ").click( function(event) { jquery("#contentcomponent").load(services); } );
since line of code jquery(this).attr("id") gives me id, hoping work [it did not work]:
var services = "index.php?option=com_content&view=article&id=2&tmpl=component"; var designs = "index.php?option=com_content&view=article&id=3&tmpl=component"; var contact = "index.php?option=com_content&view=article&id=2&tmpl=component"; jquery(".ajaxmenu > li > ").click( function(event) { jquery("#contentcomponent").load(jquery(this).attr("id")); } );
i create if, or loop, iterate through each var, (i.e. if jquery(this).attr("id") == "services"...load(services))
but suspect can accomplish simple elegant code.
how translate jquery(this).attr("id") .load() function without having if, loop, etc., value of jquery(this).attr("id") against named variables stored urls loaded?
i firstly set href attributes links seems place set them.
<ul class="ajaxmenu"> <li><a href="...">services</a> ... </ul>
and in click handler, load phone call using href attribute. , don't forget phone call preventdefault
stop click reloading entire page.
jquery(".ajaxmenu > li > ").click(function(e) { var href = jquery(this).attr('href'); jquery("#contentcomponent").load(href); e.preventdefault(); });
jquery
Comments
Post a Comment