Solved

JavaScript/jQuery: Reorder input/textarea by value

Posted on 2013-02-04
4
563 Views
Last Modified: 2013-02-05
Using JavaScript / jQuery, how can I sort a group of an input and a textarea by the value of the textarea?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

 $('#alpha').click(function() {
  alert("Change Order to Apple, Peach, Zoo");
 });

});

</script>
</head>
<body>


<div id="fields">
    <p class="item">
        <a class="itemDown"></a>
        <a class="itemUp first"></a>
        <input autocomplete="off" name="Text_healthcare_Client_List_xa0" value="2400195" type="text"><br />
        <textarea rows="9" cols="99" name="Text_healthcare_Client_List_xb0">Zoo</textarea><hr />
    </p>
    <p class="item">
        <a class="itemRemove"></a>
        <a class="itemDown"></a>
        <a class="itemUp"></a>
        <input autocomplete="off" name="Text_healthcare_Client_List_xa1" value="2402014" type="text"><br />
        <textarea rows="9" cols="99" name="Text_healthcare_Client_List_xb1">Apple</textarea><hr />
    </p>
    <p class="item">
        <a class="itemRemove"></a>
        <a class="itemDown"></a>
        <a class="itemUp"></a>
        <input autocomplete="off" name="Text_healthcare_Client_List_xa2" value="2400173" type="text"><br />
        <textarea rows="9" cols="99" name="Text_healthcare_Client_List_xb2">Peach</textarea><hr />
    </p>
</div>

<input type="button" id="alpha" value="Sort" />

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[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
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38853569
Test page : http://jsfiddle.net/Kp9FD/
Using :
$(document).ready(function() {

 $('#alpha').click(function() {
     $(".item").each(function() {
         sortDem($(this).prevAll(".item:eq(0)"), $(this))
     })
 });

});

function sortDem(prev, curr) {
    if(prev.length>0) {
        var pVal = $("textarea", prev).val();
        var cVal = $("textarea", curr).val();
        if(pVal>cVal) {
            curr.insertBefore(prev);
            sortDem(curr.prevAll(".item:eq(0)"), curr); 
        }
    }
}

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 38854893
Thanks!  I modified your code slightly to make it case-insensitive.


function sortDem(prev, curr) {
    if(prev.length>0) {
        var pVal = $("textarea", prev).val().toLowerCase().trim();
        var cVal = $("textarea", curr).val().toLowerCase().trim();
        if(pVal>cVal) {
            curr.insertBefore(prev);
            sortDem(curr.prevAll(".item:eq(0)"), curr); 
        }
    }
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38854924
0
 
LVL 16

Author Comment

by:hankknight
ID: 38857028
Ooops, the order is not preserved when the form is submitted.  I asked a related question here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28021103.html
0

Featured Post

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

738 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