CSS "id" and "class" syntax in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BobBlock
    New Member
    • Jul 2009
    • 7

    CSS "id" and "class" syntax in javascript

    I have a box defined like this:

    Code:
    <div id="shadow-container1">
    <div class="shadow1">
    <div class="shadow2">
    <div class="shadow3">
    <div class="container">
    the innermost "container" is where content appears.

    I have to pass the id of "container" to the javascript variable parameter
    "target":

    Code:
    var countries=new ddajaxtabs("source", "target")
    How do I replace "target" with the full id of "container" , that is what would the correct syntax for "target" be?

    I tried many variations but could not come up with one that works, tho this must be a simple one for the experts here.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    If it's as you've described, it should be "shadow-container1". If that doesn't work, give a link to the Ajax-tab code that you're using.

    Comment

    • BobBlock
      New Member
      • Jul 2009
      • 7

      #3
      acoder,

      As "target" has to resolve to "#shadow-container1 .container", the # alone does not work.

      I don't understand what you mean by "give a link to the Ajax-tab code that you're using".

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        I did a quick search and found that you're using the Dynamic Drive Ajax Tabs. OK, you need to pass the ID, so the easiest way is to give the container a unique ID:
        Code:
        <div class="container" id="container">
        and pass that ID.

        Comment

        • BobBlock
          New Member
          • Jul 2009
          • 7

          #5
          So I gave every div in the group a unique ID, doing away with the "class"es alltogether. This made the script happy!

          Does this mean id+class combinations cannot be used in JS in general, or is this peculiar to the script in question?

          Thank you and cheers.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            They can be used together, but the script required a unique ID. IDs are unique whereas classes are not. Sometimes you want some CSS rule/script to apply to a set of elements, so you use one class for all of them, but you also want to refer to a particular element for some rule and giving a unique ID makes sure of that. You can refer to a particular element without IDs, e.g. by using getElementsByCl assName() and then indexing the correct one, but in this case, that wouldn't have worked.

            Comment

            • rnd me
              Recognized Expert Contributor
              • Jun 2007
              • 427

              #7
              Originally posted by BobBlock
              So I gave every div in the group a unique ID, doing away with the "class"es alltogether. This made the script happy!

              Does this mean id+class combinations cannot be used in JS in general, or is this peculiar to the script in question?

              Thank you and cheers.
              jQuery supports mixed searches just like css.

              Comment

              • BobBlock
                New Member
                • Jul 2009
                • 7

                #8
                Originally posted by acoder
                They can be used together, but the script required a .....
                Thank you again, acorder. Cheers.

                Comment

                Working...