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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    175
    Thanks
    14
    Thanked 1 Time in 1 Post

    JQuery Passing variable to selector

    Hi,
    I am trying to pass a variable to a selector through a function call.

    The issue involves the dd element following the dt element.

    html:

    Code:
    <div id="lks">
    <dt class="consumer"><a href="http://www.bu.edu/prj/spring2004/solomon-etal.html">Boston University's Psychiatric Rehabilitation Journal, Spring, 2004</a></dt><dd>Phyllis Solomon authored "Peer Support/Peer Provided Services Underlying Processes, Benefits, and Critical Ingredients"</dd>
    
    <dt class="groups"><a href="http://www.chadd.com">Children and Adults with Attention Deficit/Hyperactivity Disorder</a></dt><dd>CHADD improves the lives of people affected by AD/HD.</dd>
    </div>
    The script so far:

    Code:
    $("#consumer").click(function(){
    	showlink("consumer");
    	});
    function showlink(item){
    	 $("dt." + item).css("border", "3px double red");
    	 $("dt.consumer + dd").css("border","3px double red");
    I am successful in passing the variable 'item' to the first selector but not the second.

    The second selector fires correctly if I hard code the class name but I can not get it to work with the variable.

    These are the variations I have tried to no avail:

    Code:
    //$("'dt.+ item' + 'dd'").css("border","3px double red");   //everything red
    	//$("dt.+item+ dd").css("border","3px double red");      //no reaction
    	//$('"dt." + item + dd"').css("border","3px double red"); //everything red
    	//$("'dt.' + item + dd").css("border","3px double red"); //everything red
    	//$("'dt.' + item + 'dd'").css("border","3px double red");  //everything red
    	//$("dt.(item + 'dd')").css("border","3px double red");    //locks everything, goes in to loop
    Would appreciate comments.

    Andy

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You want to break the variable outside of any and all quotes, just as you've done in the first selector. Remember you are simply concantenating a string together: the string "dt", the value of variable item, and the string " + dd".

    Code:
    $('dt.' + item + ' + dd').css(...)
    Should work.

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    175
    Thanks
    14
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fumigator View Post
    You want to break the variable outside of any and all quotes, just as you've done in the first selector. Remember you are simply concantenating a string together: the string "dt", the value of variable item, and the string " + dd".
    Works perfectly. Thanks for the clarity in the explanation. Well said.

    Andy


  •  

    Posting Permissions

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