0

This code doesn't work

var next = $("#orders").find(".next");
            if (next.length == 1) {
                var address = $(next[0]).find(".directionsAddress");
                var destination = $(address[0]).text();
            }

It is suppose to find one div with a class of "next" that I know exists on the page, then within that one item of the result set array, there will be one div with a class name of directionsAddress. The "next" array is coming back with a length of 1, so it looks like the problem is with my $(next[0]).find because the address array is coming back as 0 length and I am making a syntax error of some sort that I don't understand.

2
  • Please post your HTML Commented Aug 20, 2015 at 19:11
  • 1. You can simplify your code to this: var destination = $('#orders .next:first .directionsAddress:first').text(). 2. If your element exists it will return the text. 3. Don't forget to wrap all with $(document).ready(). Commented Aug 20, 2015 at 19:11

1 Answer 1

0

are you looking to do something like this?

$(document).ready(function() {
	alert($('#orders div.next:first div.directionsAddress:first').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "orders">
    <div class="next">
        <div class="directionsAddress">THIS IS WHAT I WANT</div>
        <div class="directionsAddress">This is not the one I want</div>
    </div>
    <div class="next">
        <div class="directionsAddress">This is not the one I want</div>
    </div>
    <div class="next">
        <div class="directionsAddress">This is not the one I want</div>
    </div>
</div>

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

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.