0
<%= semantic_form_for :.........  do |f| %>
<%= f.inputs do%>
    <%= pluralize @size, 'Profitable Routes to test'%>
    <p>User id: <%= @id %><p>
    ....

<title> audio player</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="/skin/pink.flag/jplayer.pink.flag.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">
    $(document).ready(function(){
        var u_id = document.getElementById('id')
        $("#jquery_jplayer_1").jPlayer({
            ......
                ......
                ......
                .......
        });
    });
        //]]>
    </script> 

I want to pass the User id to javascript, i'm trying to do it with this: var u_id = document.getElementById('id') but it says that u_id is null, who can i pass it? Thanks!

2
  • document.getElementById('id') will give you the DOM element that has the ID id, not the actual element ID Commented Mar 13, 2013 at 13:43
  • In the event the script is going to be properly separated into a file and referenced in a script tag within the header, write your required data either into a hidden field or into an elements data attribute. That way you can use just the script to access what you need instead of hacky inline-script. If the hidden field is for example has an id of "userId" you can then do something similar to $("#userId").val() to get the user id. Commented Mar 13, 2013 at 13:56

3 Answers 3

4

You should write the variable directly in the JavaScript code:

var u_id = <%= @id %>;

Actually when you use document.getElementById() in JavaScript you get a DOM element. And not the variable @id. The variable @id doesn't event exist for JavaScript.

Why?

I guess you're using Rails. @id is a Rails variable. Rails compiles the templates (on the server) before sending the final html page to the user. It means it replaces all the <% %> by the results of each block which are plain text.

The JavaScript is then run on the client browser. It's not aware of Rails.

When you do var u_id = <%= @id %>; Rails compiles it in something like var u_id = 198297; which is send to the client browser. Then JavaScript is happy, the variable is correctly set.

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure about the exact syntax but would using <input type="hidden" id="userId" value=@id/> in the page and var userId =$("#userId").val() in the script file not work too and keep the layers separated? I'm assuming this for a flexible solution where the script might not be embedded in the page but instead is in a separate file.
Should work. But you can as well use the JavaScript global scope, just define the variable in this page with <script>var u_id = <%= @id %>;</script> and use the u_id variable everywhere else in JavaScript, even separate files. I wouldn't add an html field just for this purpose.
@Nope imho your comment is answer worthy... I'd give you a +1. I'm maintaining a prog and due to security settings I'm not allowed to execute in-age scripts. Your suggestion works for me.
0

document.getElementById('id') is the element, not its value. You should use something similar to

id = document.getElementById('id').innerHTML;

Comments

0

u_id is the dom element . you should select a attr of this element

u_id.value

or

u_id.innerHTML

in jquery :

u_id.val()
u_id.html()

1 Comment

That's not going to help if u_id is undefined because there's no element with an id of 'id'; trying to access a property of it would throw an error.

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.