Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts

    how to add variable in javascript function

    I am using Jquery but the question belongs to simple JS



    PHP Code:
    $("#anyID > tbody"
    Now this works fine where # comes before any ID

    The problem is if i have anyID in JS variable like

    PHP Code:
    var1"#anyID"
    How can i insert that in above function

    PHP Code:
    $(var1 "> tbody"
    ; is not working
    Please de-reputate me
    © 0o0o0o0

    Its better to rule in Hell then to serve in Heaven

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts
    the two strings are different...

    does jQuery need the space between "D" and ">" ?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Did you try to print the variable, to be sure it's storing properly?

    Did you try storing the other text into a variable?

    For instance:

    PHP Code:
    var1 "#anyID"
    var2 "tbody"
    $(var1 var2
    Or maybe just try:

    PHP Code:
    $(var1 "tbody"
    Technically, that's a string, and since it's a string, it cannot have a numerical value.

    So you'll have to associate a numerical value to it.

    so instead of being just tbody, it'd be:

    PHP Code:
    tbody 
    And then you'd simply remove the quotes, and plus sign, and it should work.


    Quote Originally Posted by rnd me View Post
    the two strings are different...

    does jQuery need the space between "D" and ">" ?
    True, but even if they are different languages, a numerical value is a numerical value is a numerical value. As long as you call the variable properly inside of the code you want to compare in, it doesn't matter if it's php, js, asp, or whatever.

    It probably doesn't need the space, I don't know too much about jQuery, but I'd assume that the context above is neater than cramping the code entirely together, making it more difficult to troubleshoot.
    Last edited by Deacon Frost; 06-03-2009 at 09:04 AM.

  • #4
    Regular Coder Actinia's Avatar
    Join Date
    Feb 2007
    Location
    Essex, UK
    Posts
    250
    Thanks
    1
    Thanked 23 Times in 22 Posts
    I would guess that an expression like
    Code:
    (var1 > var2)
    or
    Code:
    (var1 > "tbody")
    are seen by Javascript as comparisons, and would yield a boolean value. My guess is that you should be using string concatenation such as:
    Code:
    $(var1 + " > tbody")
    or
    Code:
    $(var1 + " > " var2)
    .

    As suggested, you should try inserting an alert to confirm what you thing is the right expression, or better still, use firebug's console.log.

    John

  • #5
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts
    thanks guys its working now
    Please de-reputate me
    © 0o0o0o0

    Its better to rule in Hell then to serve in Heaven


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •