?
Solved

jQuery Find Function Element Order

Posted on 2013-02-07
7
Medium Priority
?
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

800 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