Shanison
Shanison

Reputation: 2295

jQuery get element's attribute 'loop'

I try to get an attribute of a div element using jQUery 1.6.2

html

<div id="scroller" loop="2"></div>

js

$('#scroller').attr('loop') 

The above js would always return undefined.

I try to upgrade jquery to 1.6.4, then the same js returns me the word 'loop'. Is this loop attribute a reserved attribute?

I do this becoz of using a library for scrolling text.http://remysharp.com/2008/09/10/the-silky-smooth-marquee/

Upvotes: 1

Views: 999

Answers (3)

zzzzBov
zzzzBov

Reputation: 179046

What's really going on is jQuery's implementation of attr. loop is a boolean attribute for media (<audio> and <video>) elements, so its presence indicates that the audio or video should loop.

When boolean attributes are used, it is invalid to specify a value other than the attribute name itself. When jQuery checks a boolean attribute, it returns the name of the attribute itself. In this case [loop]'s value is its name: "loop".


You shouldn't be adding custom attributes to elements unless you're using the [data-] attributes specified in HTML5:

<div id="scroller" data-loop="2"></div>

jQuery supports accessing these [data-] attributes with the data function:

$('#scroller').data('loop'); //returns "2"

Upvotes: 3

Sir Hally
Sir Hally

Reputation: 2358

Maybe, "loop" is some predefined value into jQuery. So, it is a jQuery bug.

Use simple JS for this:

document.getElementById("scroller").getAttribute("loop")

Upvotes: 1

xdazz
xdazz

Reputation: 160833

It seems that it is a bug.

document.getElementById("scroller").getAttribute("loop") will return 2.

Upvotes: 0

Related Questions