jquery - pass variable from html to javascript - Stack Overflow

<%= semantic_form_for :.........do |f| %><%= f.inputs do%><%= pluralize @size, '

<%= 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!

<%= 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!

Share Improve this question asked Mar 13, 2013 at 13:41 AnnaAnna 2033 gold badges8 silver badges23 bronze badges 2
  • document.getElementById('id') will give you the DOM element that has the ID id, not the actual element ID – letiagoalves 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. – Nope Commented Mar 13, 2013 at 13:56
Add a ment  | 

3 Answers 3

Reset to default 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 piles 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 piles it in something like var u_id = 198297; which is send to the client browser. Then JavaScript is happy, the variable is correctly set.

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

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

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()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744967938a4603795.html

相关推荐

  • jquery - pass variable from html to javascript - Stack Overflow

    <%= semantic_form_for :.........do |f| %><%= f.inputs do%><%= pluralize @size, '

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信