5

I'm struggling with some Javascript in my first rails application.

Partial: _care_point.html.erb

<script>
  $(function() {
    $( ".draggable" ).draggable({grid: [50, 20]});
    $( ".node_input").each (function() {
      $(this).hide();
    });
    $("#<%="node.#{care_point.id}" %>").live('dblclick', function(){
      console.log('moo');
      jQuery(this).hide();
      jQuery('.node_input', jQuery(this).parent()).show();
    });
  });
</script>
<div id=<%="care_point.#{care_point.id}" %> class='draggable node_chin'>
  <div id=<%="node.#{care_point.id}" %> class='node'><%= care_point.body %>
  </div>
  <textarea class='node_input'><%= care_point.body %></textarea>
</div>

This is the output:

    <script> 
  $(function() {
    $( ".draggable" ).draggable({grid: [50, 20]});
    $( ".node_input").each (function() {
      $(this).hide();
    });
    $("#node.1").live('dblclick', function(){
      console.log('moo');
      jQuery(this).hide();
      jQuery('.node_input', jQuery(this).parent()).show();
    });
  });
</script> 
<div id=care_point.1 class='draggable node_chin'> 
  <div id=node.1 class='node'>Moo foo
  </div> 
  <textarea class='node_input'>Moo foo</textarea> 
</div>

I first added the dblclick event listener classbased, but that caused it to be added multiple times. This made me change it to a id based approach, but now it does not work. Is it because I try to dynamically build up the id?

Is this even the righ place to do this kind of thing?

7
  • Can you print what is outputted to the browser (use 'view source' on any browser) to see what has been put in place of variables? Commented Jun 15, 2011 at 9:45
  • @Nicola Peluchetti - I've updated my post Commented Jun 15, 2011 at 9:53
  • It's considered bad practice to dynamically generate javascript. Use static packaged javascript files. Commented Jun 15, 2011 at 9:55
  • @Raynos - I don't even know what that means. Could you explain it in a post? Commented Jun 15, 2011 at 9:56
  • Views should contain HTML not javascript. Include your javascript in your layout view / master page. Commented Jun 15, 2011 at 9:57

2 Answers 2

3

Try the following:

$("#<%="node.#{care_point.id}" %>").live('dblclick', function(){
      console.log('moo');
      jQuery(this).hide();
      jQuery('.node_input', jQuery(this).parent()).show();
    });
Sign up to request clarification or add additional context in comments.

6 Comments

+1. @willcodejavaforfood, you've got your quotes in the wrong place.
Still does not work, maybe Raynos has a point. This is the wrong place for Javascript. It worked until I made it dynamic.
Raynos point is valid but should'nt really stop your code working. After making the above change can you show me the source/html being generated using the same 'View Source'. It should render > $('#node.2').live('dblclick', function(){ as Nicola mentioned.
I've updated my post with your latest code and the new output
shux. pretty dumb of me. you can't use . in your html id. In jQuery selectors, dot notation specifies class. So basically when we ask jQuery to find '#node.2', it goes and find div with the id id=node and with class class=2. You can replace the dot with lets say underscore $("#<%="node_#{care_point.id}" %>")
|
2

The problem is here:

 $("#'#node.2'").live('dblclick', function(){

To work it must be:

 $('#node.2').live('dblclick', function(){

i'm no expert of ruby but you must change something here:

 $(<%="'#node.#{care_point.id}'" %>).dblclick(function(){

I'd try (but i'm guessing - edited)

$('#<%=node.#{care_point.id} %>').dblclick(function(){

EDIT - Try removing the dots in the ids of th HTML: look at this fiddle http://jsfiddle.net/JeHuD/

replace

#<%=node.#{care_point.id} %>

with (both in the jquery selector and in the HTML (also consider that your id in the html should have double quotes like this: id="node1")

#<%=node#{care_point.id} %>

FINAL EDIT - in the jquery selector you must escape dots withdoubole backslashes: here is the updated fiddle that works with the dot http://jsfiddle.net/JeHuD/1/

4 Comments

Thank you Nicola, but the event is still not triggered
the problem is in the html i think try removing the dots in th id and it should work
That was it, thank you so much. I guess the dot is in illegal character in an id?
No it's not. I checked and the problem is in jquery: if there is a dot you must escape it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.