Anyone jQuery experts here can help me convert the follwing javascript codes to jQuery? Although it works I’d rather see hows it’s done with jQuery
function loadit(element)
{
document.getElementById('container').src = element.rel;
var tabs = document.getElementById('tabs').getElementsByTagName("a");
for (var i=0; i < tabs.length; i++)
{
if(tabs[i].rel == element.rel)
{
tabs[i].className = "selected";
}
else
{
tabs[i].className = "";
}
}
}
Here is the HTML
<div id="tabs">
<ul>
<li><a href="#" rel="/root/a" onClick="loadit(this)" class="selected">A</a></li>
<li><a href="#" rel="/root/b" onClick="loadit(this)">B</a></li>
<li><a href="#" rel="/root/c" onClick="loadit(this)">C</li>
</ul>
</div>
<iframe id="container" name="container"></iframe>
And here is how I populate the container on windowload:
function startit()
{
var tabs = document.getElementById('tabs').getElementsByTagName("a");
document.getElementById('container').src = tabs[0].rel;
}
window.onload=startit;
Assuming the parameter passed in (
element) is itself a jQuery object, it could look like this.Note that where ii’m dealing with a jQuery object, I tend to prefix the variable with
$to distinguish itedit: I noticed a typo in my code above. I was missing a
#from the lineHere is a very simplified live example in lieu of your HTML: http://jsfiddle.net/FKf9L/1/
Ive assumed that you’re calling
loaditwhen clicking one of thea‘s which form the tabs. If this is the case it’s a bit redundant, you should just latch onto the click event of theain jQuery. Making the code:See this live example: http://jsfiddle.net/p99Q2/
edit2: You may as well do away with the
startitmethod and just issue aclickon the firstawithin the tabs:Live example: http://jsfiddle.net/p99Q2/2/