Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,
Please Help !!!

I have a button. On the click of this button , i want all the items in my
list to get transferred to hiddenfield.

The following is working however, i need to convert it into Jquery... :



$('#<% =btnPreview.ClientID %>').click(function() {



              myDivObj = document.getElementById("list2");
      if (myDivObj) {
                document.getElementById('<%=hdnSelectedMetrices.ClientID%>').value = null;
                  for (var ii = 0; ii < myDivObj.childNodes.length; ii++) {

                      document.getElementById('<%=hdnSelectedMetrices.ClientID%>').value = document.getElementById('<%=hdnSelectedMetrices.ClientID%>').value + ',' + myDivObj.childNodes.item(ii).nextSibling.innerHTML;

         }  }
         });
      });
Posted
Updated 28-Feb-12 14:25pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Feb-12 21:45pm    
Why? And what's so interesting?

I understand, you might have been excited, but still don't shout. ALL CAPS is considered shouting on the Web, it irritates many.
--SA

"Convert JavaScript to jQuery" makes no sense: jQuery is just a JavaScript library. All you can do in your code is replacement of some DOM calls with jQuery shortcuts.

First of all, replace document.getElementById with $(selector), pretty much as you did in your first line. For example, the selector for you id="list2" will be "#list2", so you can replace the line using this id, so the whole second line will be:
JavaScript
myDivObj = $("#list2");

And so on…

And yes, you can also replace your calculations with childNodes and nextSibling, etc., with jQuery traversing.

Please see: http://api.jquery.com/category/traversing/[^].

As I can see, you know enough jQuery to comb it by yourself. If you don't waste time on it like myself — leave it as is; it's already good enough.

—SA
 
Share this answer
 
v2
Comments
shikhar gilhotra 29-Feb-12 12:06pm    
no..i am a novice... i have just picke this code from somewhere ..now i need to convert it into jquery..tried a lot of things already...but no joy..believe me....so i need help.thx.
Sergey Alexandrovich Kryukov 1-Mar-12 4:12am    
What do you mean "no"?!

I already helped you. I explained that "convert" makes no sense. When you use jQuery, you code remain JavaScript. You can just improve your code using jQuery, make it shorter. Please use my advice, and if something is difficult to improve, leave as is.

Please consider accepting this answer formally (green button).
And... do you work. If you are a novice -- gradually become experienced.

Good luck,
--SA
Hi, this code converted to JQuery is

$('#<% =btnPreview.ClientID %>').click(function() {
    myDivObj = $('#list2);
        if (myDivObj) {
            $('#<%=hdnSelectedMetrices.ClientID%>').val('');
            $.each(myDivObj.children(), function(ind, val)
            {
             $('#<%=hdnSelectedMetrices.ClientID%>').val( 
                      $('#<%=hdnSelectedMetrices.ClientID%>').val() + ',' +
                       myDivObj.find(':nth-child('+ind+')').next().html();
           });
        });
});



Hopefully it works as expected - you never told me what #list2 was exactly so this code can be improved if you supply that information..
Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900