3

I have string that contains some div's how to get one of them by id ?

EDIT

I'm using right click selection plugin, so when i select a div .

 $("#first #second").contextMenu({
        menu: 'myMenu'
    },
        function(action, el, pos) {
      switch (action) {
            case "do":
                {

and the selection is basicley

$(el).html()

So this is the code that contains the html that is in #second, but the #second isn't one div , it is a'lot of divs with that id, and i want when i click on that div to select certain id and get it's context . Is there a easyer way ?

4
  • Can you please provide a code sample, so we can see how you are getting the string containing the divs. Commented May 20, 2011 at 16:24
  • A string? So you're not working with the DOM? Do you want the answer in the form of a regular expression? I guess I'm asking, please provide more detail. Oh, and welcome to stack overflow :) Commented May 20, 2011 at 16:24
  • In other words, do you need help parsing a string? Commented May 20, 2011 at 16:29
  • -1 You need to provide some more detail if you hope to get an answer. For example a sample HTML fragment would be a good start. Note: you can edit your question Commented May 20, 2011 at 16:30

2 Answers 2

4

You can have jquery interpret a string as a set of tags like this:

$(htmlstring);

So something like this might work:

$("<div><div id='test'></div></div>").find("#test");

Edit: Now with the updated question this is of course not what you want.

You can't have more than one element with the same id. IDs are supposed to be unique identifiers.

Sign up to request clarification or add additional context in comments.

5 Comments

well since the OP has changed the question it looks like we both wasted our time :) but for reference filter is better than find here as filter doesn't require an outer container.
and i'll +1 you anyway as you made a good answer before the question was changed.
true, +1 for that. it's funny to see the answers, all somehow trying to interpret the question which turned out to be completely different ;-)
+1 for using filter, saved me, even though not relevant to question it was relevant to my google search ;)
+1 because although it doesn't answer the question, it answers exactly what I was looking for. Saved me a lot of time :-)
1

In your case you can wrap el and traverse from it, like this:

$(el).find('.something-else').blah(); // ...

el is not a string, it's a DOM element.

Some of your confusion might stem from writing $(el).html(). If you were debugging with that, it'd look like a string because you were extracting the actual HTML held within the DOM element you had in hand.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.