2

I can't modify this HTML:

<li data-corners="false" data-shadow="false" data-iconshadow="true" data-rapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c">
    <div class="ui-btn-inner ui-li">
        <div class="ui-btn-text"> 
            <a class="make-inline ui-link-inherit" href="#"> 
                <h3 class="ui-li-heading"> Attending: 
                </h3> 
            </a> 

            <div class="ui-select">
                <a href="#" role="button" id="undefined-button" aria-haspopup="true" aria-owns="undefined-menu" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-icon="arrow-d" data-iconpos="right" data-theme="c" data-inline="true" data-mini="false" class="ui-btn ui-btn-up-c ui-btn-inline ui-shadow ui-btn-corner-all ui-fullsize ui-btn-icon-right ui-disabled" aria-disabled="true">
                    <span class="ui-btn-inner ui-btn-corner-all">
                        <span class="ui-btn-text"> Maybe </span>
                        <span class="ui-icon ui-icon-arrow-d ui-icon-shadow">&nbsp;</span>
                    </span>
                </a>
                <select class="attending 310 mobile-selectmenu-disabled ui-state-disabled" data-inline="true" data-native-menu="false" tabindex="-1" disabled="disabled" aria-disabled="true">
                    <option value="2"> Yes </option>
                    <option value="0"> No </option>
                    <option value="1"> Maybe </option>
                </select>
            </div>
        </div>


        <span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>


    </div>
</li>

The select tag contains class="attending", which is unique to this particular page, so I'm using that to gain access to this part of the code. Eventually, I'm trying to access the last span tag with class="ui-icon-arrow-r", so I can style it. I've tried using jQuery like so:

$(".attending").parent().find(".ui-icon-arrow-r").css('background-color','yellow');

It doesn't work. When I remove the find() part, it selects all of the li elements on the page. So, I know that part is working. But the find part is not and I can't figure out why not. How to select this element?

3 Answers 3

5

Try this:

$(".attending").closest('div.ui-btn-inner').find(".ui-icon-arrow-r").css('background-color','yellow');
Sign up to request clarification or add additional context in comments.

5 Comments

@sungod000 - This works: jsfiddle.net/yFgnx (though with only a non-breaking space in the span there's not much yellow).
THanks guys, but for some reason it doesn't work. In fact, I've tried a bunch of things, and they are not making sense. Maybe the DOM objects are arranged differently than the HTML? Not sure, but I'm getting unexpected results.
@sungod000 - this is the right approach. If it's not working for you, then your page isn't actually like the HTML you have in your question or you have a script error that keeps this from executing. You need to either check the actual working DOM to verify it, check for script errors or post a link to the actual page so we can help troubleshoot.
I'm new to this, but just did a bunch more testing and this works in the console. I've been told by a friend, I need to figure out when to tell the browser to execute this code. Something about event handlers and their sequence.
Ok, got it. Need to add $(document).ready(function(){ //MyCodeHere }); so it would execute after the DOM was ready.
2

Here is how I would do it:

  1. Add a class (.my-li) to your li (because we want a class-based selector, not tag-based one)

  2. Use jquery parents method:

    $(".attending").parents('.my-li').find(".ui-icon-arrow-r").css('background-color','yellow')​
    

And yep - here is your case resolved: http://jsfiddle.net/Xfj5U/

Update:

Sorry, I messed that you can not modify html - my bad :(

You still can use parents method like this (with tag selector and not modifying html):

$(".attending").parents('li').find(".ui-icon-arrow-r").css('background-color','yellow')​

http://jsfiddle.net/ETpVL/

2 Comments

The first sentence says I can't modify this HTML.
Sorry, but this answer is wrong in every possible way. OP can't modify the html, so can't add a class. Plus you're setting the colour rather than the background colour, and you're setting it red not yellow, and you're setting the colour for the whole li element not just the specified span.
0

You need to traverse two times using parent() :

$(".attending").parent().parent().find('span:last-child').css( ... )

2 Comments

I just tried, and as suspected, didn't work. Traversing once, already brings me up to the li element, which is high enough. Shouldn't the find function work from that element?
This doesn't work. You need parent().parent().parent() to get to the div containing the span with the ".ui-icon-arrow-r" class, but even then it won't work if you use 'span:last-child' rather than ".ui-icon-arrow-r" because it'll find all spans within that div that are the last child of their parent.

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.