0

I have a #pdfcontainer which is inside divs #main and #pdfmain. I'm using a variable, because I use the same div many times:

var $pdfcontainer = $('#main #pdfmain #pdfcontainer');

Inside the #pdfcontainer I have #delete which I want to access for various reasons. So how do I get it using the variable and without typing the whole $('#main ...') all over again? I don't just want to use selector $('#delete') because on my site it's much slower that way.

Thnx already!

  • Antti
12
  • 3
    You are aware that you shouldn't use the same "id" more than once, right?
    – ifaour
    Commented Jun 13, 2011 at 7:03
  • "I don't just want to use selector $('#delete') because on my site it's much slower that way." That doesn't make sense to me at all. jQuery's selector engine usually starts with the rightmost part of the selector anyway, and so it should be looking up #delete as a first step anyway (and then checking to make sure it's got the right lineage). What is it about your site that makes looking up an ID on its own somehow slower than looking it up and then walking the lineage? Commented Jun 13, 2011 at 7:06
  • 2
    @ifaour: Not shouldn't, mustn't. :-) Commented Jun 13, 2011 at 7:07
  • I wasn't aware of that.. why should i not use the same id? :(
    – AnttiQ
    Commented Jun 13, 2011 at 7:07
  • 2
    @AnttiQ: id values must be unique on a page: w3.org/TR/html5/elements.html#the-id-attribute If you want to give the same name-like-thing to multiple elements, you usually want a class. Commented Jun 13, 2011 at 7:09

5 Answers 5

1

Specify $pdfcontainer as context to your selector like this:

$("#delete", $pdfcontainer)

Make sure that you have only one item with unique ids, for example, you should have id #delete assigned to only one element.

0
0

$($pdfcontainer + " #delete") should work, if you declare your variable as a string var $pdfcontainer = "#id #id2 #id3";. Using the variable in place of the actual string doesn't mean you can't add more to the string.

0
$pdfcontainer.find('#delete');
0

try this..

$pdfcontainer.find('#delete'); 
2
  • I googled a bit and couple of sites say that .find is faster than using a context. Is this true?
    – AnttiQ
    Commented Jun 13, 2011 at 7:30
  • @AnttiQ: $("#delete", $pdfcontainer) runs through some test, and ultimately gets converted to $pdfcontainer.find('#delete'), so yes.
    – user113716
    Commented Jun 13, 2011 at 7:42
0

you can use pdfcontainer.find("#delete) enjoy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.