0

How can I pass a string variable from HTML to javascript with spaces?

For instance, I have the following:

 <a data-toggle="tab" data-productname="Testing Name"></a>

When I access the productname attribute in javascript, only "Testing" shows up. I tried to escape the variable with:

 <a data-toggle="tab" data-productname=<%= j "Testing Name" %>></a>

But that still doesn't work. What can I do here?

4
  • Funny how your example of how you want it does it the right way. :) Commented Jul 24, 2014 at 19:31
  • Wait... what's your javascript code look like? From what I can see this should work (with your first html snippet)... $('[data-productname]').data('productname') will return "Testing Name" (with a space). Commented Jul 24, 2014 at 19:32
  • See how it works here: jsfiddle.net/4THAq I don't see any problem in your HTML, so what's the JS? (Note that your second snippet actually is incorrect, and isn't putting quotation marks around the value) Commented Jul 24, 2014 at 19:35
  • I do something like this in JS: '$('#product_field').text($('#product_tabs .active a').data('productname'));'. Adding the quotes as suggested by Dave Newton fixed the problem. Commented Jul 24, 2014 at 19:47

1 Answer 1

4

Quote it.

<a data-toggle="tab" data-productname='<%= j "Testing Name" %>'></a>
Sign up to request clarification or add additional context in comments.

5 Comments

I'm curious about the downvote since it is the problem.
But it's not the problem. His first example is correct and there's no reason it shouldn't work (see my comment). His second example is incorrect, but he said his first attempt was the first example...
@nzifnab The second attempt doesn't quote the value, which will cause the error he's seeing. There's nothing dynamic in the first example, there is in the second, and in the second example, the OP neglected to quote the value, causing the error. Not quoting the value when it's coming from the Ruby code is precisely the problem.
"For instance, I have the following:" totally correct and valid HTML markup that should work properly. If he meant that he has <a data-toggle="tab" data-productname=<%="Testing Name"%>></a> or <a data-toggle="tab" data-productname=<%=some_variable%>></a>, then wouldn't he have said that? I'll agree that your fix addresses his escaped version... but that already wasn't necessary in the first place. I must be missing something.
@nzifnab Must be. I'd guess it's that the first example works, because it isn't wrong, and the second doesn't, because it is. I'm not arguing the OP couldn't have figured it out by reading his/her own question and paying attention, or that the OP wasn't unclear with their verbiage. According to the OP the lack of quotes was the problem, and I'm inclined to believe him/her.

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.