tags:

views:

28

answers:

2

I have a set of hidden inputs on my html page.

I'm looking to run a piece of jQuery to check if any of them have a particular value constant ("New"), and if they do, then do the following:

$('#dataContainer').scrollTo("id of the input with the special value");

Is it possible to loop through the inputs based on value using jQuery selectors? In this case, once it finds the first input with the value "New", it can call that scrollTo() method and then quit, it doesn't need to look at the rest of them.

How would you do this with jQuery?

+4  A: 

You can use the attribute-equals selector, like this:

$('#dataContainer').scrollTo("input[value='New']:hidden");

Internally it's really doing a $("input[value='New']:hidden", this).offset() on #dataContainer, and .offset() returns the offset of the first of the matched elements, so it'll go to the first one it finds like you want.

Nick Craver
What happens in the case that there are no inputs with "New"? How can I ensure I won't error in that instance.
FrankTheTank
@FrankTheTank - It won't scroll anywhere in that case, for example [go here](http://demos.flesler.com/jquery/scrollTo/) and try this in the console: `$('#relative-selector').unbind().click(function(){ $('#pane-target').stop().scrollTo( 'li:eq(344)', 800 ); });` you'll see the "Relative Selector" doesn't blow up.
Nick Craver
A: 

Something like $('#dataContainer').scrollTo("[attr=NEW]");

mcandre
How does this work?
patrick dw