Solved

JavaScript/jQuery: Reorder VALUES of input/textarea by value

Posted on 2013-02-05
3
340 Views
Last Modified: 2013-02-07
The problem with this code is that the name attribute of the input tag and textarea tag get moved along with everything else in the paragraph so when I save the results the order does not actually save.  

For example, the first textarea should look like this:
<textarea rows="9" cols="99" name="Text_healthcare_Client_List_xb0">Apple</textarea>

Not this:
<textarea rows="9" cols="99" name="Text_healthcare_Client_List_xb1">Apple</textarea>

How can the order of the values be changed but the name attribute be preserved?

<!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() {
     $(".item").each(function() {
         sortDem($(this).prevAll(".item:eq(0)"), $(this))
     })
 });


});

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); 
        }
    }
}

</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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38857065
test page : http://jsfiddle.net/Kp9FD/2/

$(document).ready(function() {

 $('#alpha').click(function() {
     $(".item").each(function() {
         sortDem($(this))
     })
 });

});

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

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 38861131
The problem with that code is that it does not change the input that is associated with it.

In the example, "Zoo" is associated with "2400195" but after you sort it, it incorrectly becomes associated with "2400173"
0
 
LVL 16

Author Comment

by:hankknight
ID: 38864427
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Non-Resizable Pharagraph 2 17
Can't get js code to execute 8 30
null, truthy, falsey confusion 25 28
How do I post more than 1 item to php backend 24 27
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…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

685 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