Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2012, 07:09 PM   PM User | #1
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
How to find an element based on its attribute

Hello,

I have a <ul> element in which I use JS to place a number of <li> elements. These <li> elements have an attribute called 'index' which I use to jump around to different html pages.

HTML page.
Code:
<div id="scrubber">
			<ul id="scrubberList">
			<!--This is where the tick marks go for the page scrubber on the console.-->
			</ul>
</div>

Code:
function createScrubberTicks(){
	for(i=0; i< pages_arr.length; i++){
		$('#scrubberList').append('<li><span id="' + pages_arr[i].title + '" index="' + i + '" class="scrubberBtn">'</span></li>');
	}

    $('.scrubberBtn')
        .click(function(){
			$(this).removeClass("scrubberBtn").addClass("scrubberBtnSelected").parent().siblings().children().removeClass('scrubberBtnSelected').addClass('scrubberBtn');
			current_pg_ind = this.getAttribute("index");
			$("#dev_current_pages").text(parseInt(current_pg_ind) + 1);
			requestPage(current_pg_ind);
        })
}
And now I need to find the <li> element with the correct index so that I can remove/add the class similar to above. The alert below does not work by the way.

How can I reference the correct <li> element based on this index number? (There can be up to 80 of these <li> elements)
Code:
function setScrubberIndex()
{
	alert($("#scrubberList").getAttribute("index"));
}
m2244 is offline   Reply With Quote
Old 10-02-2012, 07:37 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
1. I think your append string is badly formed - shouldn't it be like this?
Code:
$('#scrubberList').append('<li><span id="' + pages_arr[i] + '" index="' + i + '" class="scrubberBtn"></span></li>');
2. I think you are better off using the native jQuery attr
Code:
alert($("#scrubberList").attr("index"));
3. The scrubberList has no index, it is the li's that you append to it. Have a look at DOM transversal methods like nextAll() and children()
xelawho is offline   Reply With Quote
Old 10-03-2012, 05:31 PM   PM User | #3
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Thanks for the help.

In case anyone needs this, here is what I ended up using.

Code:
	$("#scrubberList li span[index]").filter(function() { return $(this).attr('index'); }).each(function()
	{
		//alert("In the wierd function.");
		if($(this).attr("index") == parseInt(current_pg_ind))
		{
			$(this).removeClass("scrubberBtn").addClass("scrubberBtnSelected").parent().siblings().children().removeClass('scrubberBtnSelected').addClass('scrubberBtn');
			return false;;
		}
	});
m2244 is offline   Reply With Quote
Old 10-04-2012, 07:26 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Code:
$("#scrubberList li span[index='"+ parseInt(current_pg_ind)+"']")
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:37 PM.


Advertisement
Log in to turn off these ads.