ishwr
ishwr

Reputation: 745

ruby on rails : check table value from javascript

How people can check the value of a table from javascript. I am using ruby on rails. I did this :

$(document).ready(function() {
  if ("<%= @player.name %>" == "" ) {
   //show some divs
  } 
});

But it doesn't work. Even the player's name is empty, the divs don't showed. I want to check if the player's name is empty, then it should show some divs. Thanks.

Upvotes: 0

Views: 113

Answers (1)

Mischa
Mischa

Reputation: 43298

The first problem is that you're using <% instead of <%=. Secondly, suppose @player.name is john then your javascript code will be:

$(document).ready(function() {
  if (john == "") {
   //show some divs
  } 
});

See the problem? Your @player.name will be regarded as a javascript variable (which doesn't exist), not as a string.

Lastly, if your @player.name should contain a double-quote your javascript will be broken again, so you have to escape it with escape_javascript.

Solution:

$(document).ready(function() {
  if ("<%= j @player.name %>" == "") {
   //show some divs
  } 
});

Upvotes: 1

Related Questions