0

I've been trying to get the following to work: I want a jQuery function to load a specific partial when there's being clicked on a certain div with an ID. I want this ID to be part of the :locals which i'm using together with the partial.

$("#main>.dots").live("mousedown", function(e) {
        $num = 8;
        $("#main").append("<%= escape_javascript(render :partial => 'projects/show',     :locals => {:i => "#{$num}"}).html_safe %>")
    })

So far I haven't been able to get it right. The code above returns the 'called id for nil' so the $num isnt properly given to the escape_javascript function. When I replace $num with the integer 8 it works (between the <% %> tags).

Any solutions?

the syntax:

$("#main").append("<%= escape_javascript(render :partial => 'projects/show', :locals => {:i => 8}).html_safe %>")

works, so thats the way i want to go, except the 8 being jQuery ofcourse

Flame
  • 6,663
  • 3
  • 33
  • 53
  • 1
    `.live()` is depreciated (and very slow) – Popnoodles Jan 13 '13 at 21:37
  • thx for the reply I'll replace them with 'on()' :) (forgot it was deprecated) – Flame Jan 13 '13 at 21:43
  • Is there a specific reason you're using mousedown instead of click? – Popnoodles Jan 13 '13 at 21:44
  • no im changing it to 'click' now but a part of my code doesnt seem to work now. it must all be encased in '$(function() { //code }' right? – Flame Jan 13 '13 at 21:57
  • I believe ive found an answer already: jquery can only assign events when classnames already exist, and a lot of my functions are aimed at HTML thats not yet to be loaded on the page. Im reverting back to .live() since that seems to work the best. <-- I'll definitely look for an alternative when i see time – Flame Jan 13 '13 at 22:08
  • ok fixed it all with a comment on http://stackoverflow.com/questions/8021436/jquery-1-7-turning-live-into-on all fixed up now :) thx for the sidenote – Flame Jan 13 '13 at 22:21
  • escape_javascript() is also available through the alias j() – Andreas Lyngstad Jan 13 '13 at 23:06
  • I knew that but it needs to be understandable for some other people, so short code isnt a requirement :) – Flame Jan 13 '13 at 23:29

1 Answers1

0

I've fixed this by doing

var dataString = "projectid=" + $(this).attr("id") + "&left=" + $(this).css("left") + '&top=' + $(this).css("top");
    $.ajax({
        url: "/projects/show",
        type: "get",
        data: dataString,
        success: function(data) {
            $("#main").append(data)
        }
    });

So basically I went AJAX so I could send POST-data to the right controller action :)

Flame
  • 6,663
  • 3
  • 33
  • 53