[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jQuery Find Function Element Order

Posted on 2013-02-07
7
Medium Priority
?
365 Views
Last Modified: 2013-02-15
Is it possible to get a numeric position of an element using the find function?  I have a div that contains multiple checkboxes.  I want to get a number for each checkbox either its position in an array or list.  Or as it finds it I want to assign it a value.  I guess I could do this without the find function but I wanted to know what the find function was able to do as it finds each checkbox.
0
Comment
Question by:ws11
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38865609
It is not clear what you ask.

If you find input type checkbox, you will get an enumerable object back with each checkbox in the order it was found.

You can use the .eq selection to get at specific index
0
 

Author Comment

by:ws11
ID: 38865661
I am doing a $("divid").find(":text").prop("value", ?)

I want to put the ordinal value in the ? or the numeric order of the element in the dom.

It needs to be dynamic.  This is probably not possible using the find function.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38865858
I do not understand what you just said. Show some HTML or creat a jsfiddle.net
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ws11
ID: 38865894
So it would be as follows.

$("div_id").find("input:text").each(function ()  {
    x += 1;
    $(this).prop("value", x);
}

But there still is a problem.  The text boxes are in a two column table.  So the dom order is vertical since the textboxes are in two td tags per row.  I want to number going vertical down each column.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 38867457
Here

<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function() {
  var x=0;
  var inputs = $("#div_id").find("input:text");
  var y = Math.ceil(inputs.size()/2);
  inputs.each(function (i, input)  {
    $(this).prop("value",(i % 2 == 0)?++x:++y);
  });
});  
</script>
</head>
<body>
<div id="div_id">
<input type="text" value="" /><input type="text" value="" /><br>
<input type="text" value="" /><input type="text" value="" /><br>
<input type="text" value="" /><input type="text" value="" /><br>
<input type="text" value="" /><input type="text" value="" /><br>
<input type="text" value="" /><input type="text" value="" /><br>
<input type="text" value="" /><input type="text" value="" /><br>
<input type="text" value="" /><input type="text" value="" /><br>
<input type="text" value="" /><input type="text" value="" /><br>
</div>
</body>
</html>

Open in new window

result
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38875833
Did that help you?
0
 

Author Closing Comment

by:ws11
ID: 38893328
Yes I had something similar.  Sorry for the delay.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 19 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question