Solved

jQuery Find Function Element Order

Posted on 2013-02-07
7
361 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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