0

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.

2
  • 1
    escape_javascript? Commented Jul 2, 2013 at 8:18
  • would you mind to elaborate? Commented Jul 2, 2013 at 8:21

1 Answer 1

1

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
  } 
});
Sign up to request clarification or add additional context in comments.

4 Comments

sorry my mistake. I did quote the <% @player.name %>. I'll edit my question
@takodil Please look at the other remarks. This answer should still solve your question.
yes, I tried it already. The divs still doesn't showed, but when I tried it with alert, it worked. So I think, it's another mistake then. Thanks for your answer :-)
@takodil You're welcome. Good luck finding the actual problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.