Solved

jQuery Find Function Element Order

Posted on 2013-02-07
7
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
I do not understand what you just said. Show some HTML or creat a jsfiddle.net
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:ws11
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Did that help you?
0
 

Author Closing Comment

by:ws11
Comment Utility
Yes I had something similar.  Sorry for the delay.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now