Solved

jQuery Find Function Element Order

Posted on 2013-02-07
7
358 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

786 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