Using JQuery.each()

with a mix of RoR

Page 1 of 1

2 Replies - 512 Views - Last Post: 05 May 2010 - 12:55 AM

#1 jral27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-July 09

Using JQuery.each()

Posted 04 May 2010 - 07:22 PM

I'm quite new to JQuery, in fact, I'm new to Javascript in general. I've been working on this code for some time now, and I just can't get it.

My question is, am I passing the Ruby array correctly in this code? And, am I also using the .each() function correctly?
I already read about this on some other thread, but even after I did what they did, it still didn't work for me.

$(document).ready(function(){
    var myVar = [];
    myVar = <%= @x_arr.to_json %>;
    
    $.each(myVar, function(intIndex){
      $("." + this).sortable({
        connectWith: ".connectedSortable_" + this,
        cancel: '.state-disabled'
      }).disableSelection();

      s$("." + this + "li").disableSelection();
      };
    });
  });


Is This A Good Question/Topic? 0
  • +

Replies To: Using JQuery.each()

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Using JQuery.each()

Posted 04 May 2010 - 09:42 PM

I am not sure you want the "s" there before the $ symbol. Also if you are trying to select the <li> elements of any elements which have a class of ("." + this), then you may need a space before the "li".

I am not quite sure, but you could try something like...

$("." + this + " li").disableSelection();



So if one index value of your array is "mytest" it would result in the line...

$(".mytest li").disableSelection();



Hopefully that is what you are trying to do here. But as far as putting in a Ruby array into JQuery, it should be working assuming the Ruby json is correct and what you expect it to be.

:)
Was This Post Helpful? 0
  • +
  • -

#3 jral27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-July 09

Re: Using JQuery.each()

Posted 05 May 2010 - 12:55 AM

Wow, thank you. I didn't really notice all those typos.

Here's how I fixed it though:
:javascript
  var myVar = new Array(#{x_arr.size});
  myVar = #{x_arr.to_json};
  
  $(document).ready(function(){
    $(myVar).each(function(index, obj){
      $("." + this).sortable({
        connectWith: ".connectedSortable_" + this,
        cancel: '.state-disabled',
      }).disableSelection();
    });
  });


I added the array size, and removed some parts of the code that I thought didn't really make any difference :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1