1

I have the following HTML:

<div data-name="countrySelectorRoot">
    <select>
        <option value="C1">Country 1</option>
        <option value="C2">Country 2</option>
        <option value="C3">Country 3</option>
        <option value="C4">Country 4</option>
        <option value="C5">Country 5</option>
    </select>

    <script type="text/javascript">
        // Some script which finds parent div...
    </script>
</div>

Inside script, I need code which needs to find my "countrySelectorRoot" div (var MyRoot = // Code that finds element with data-name="countrySelectorRoot").

However, I can have multiple divs with same data-name attribute and same contents, but I need to find the the specific one which is the parent of the script.

How to do this?

7
  • 2
    why aren't you doing a css class or id? Commented Jun 16, 2012 at 14:05
  • Because I can have multiple same divs inside document, I need the specific one not all of them. Commented Jun 16, 2012 at 14:07
  • @Dusan, ok... but what makes one more specific that the other? Commented Jun 16, 2012 at 14:08
  • MV3 razor, I would not like to complicate with unique id or data attribute of the div if other solution is possible. Commented Jun 16, 2012 at 14:09
  • Is that <div> always an exact parent of <script> or it could be nested further? I think it would be best to use id either on <div> or <script>, otherwise it will be hard to even find the <script> selement from the script itself. Commented Jun 16, 2012 at 14:10

4 Answers 4

1

Try this

   $('div[data-name="countrySelectorRoot"][/Script]')

it shall select the the specific one which is the parent of the script.

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

Comments

0

try eq() method:

$('div[data-name="countrySelectorRoot"]:eq(0)') // this selects first div

or :has:

$('div[data-name="countrySelectorRoot"]:has(script)') 

Comments

0

This should give you an idea:

$('div script').parent()

jsBin demo

But I honestly think you're using javascript the wrong way.

Comments

0

To specifically target the one that has the script tag in it, use :has filter

$('[data-name="countrySelectorRoot"]:has(script)');

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.