1
<ul class="playlist">
    <li class="track_name">$name1
        <ul>
            <li class="play"><a class="play_link" href="$url">play</a></li>
            <li class="download"><a href="download.php?id=$url">download</a></li>
        </ul>
    </li>
    <li> ... more tracks here
</ul>
<ul class="playlist">
... more playlists here

Given a dynamically generated unordered nested lists, what is the easiest way to replace text 'play' in <li class="play"> with $name1?

I've done the following:

var val = $('.play').closest('.track_name').clone().children().remove().end().text();
$('.play_link').text(val);

This gives me all track_names, but can't figure out how to get just the relevant track name.

TIA!

0

2 Answers 2

2

You can pass a function into .text(), which will allow you to set the text of all of the .play elements:

$('.play a').text(function(i, v) {
    return $(this).closest('.track_name').clone().children().remove().end().text();
});

Code Example

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

2 Comments

I think it should be $('.play a')
Works great! Would it somehow be possible to hide that parent <li class="track_name">$name1 without disrupting the function?
0

Simply set the selected one to have a selected class on click and then use that class to get the selected one: (or do whatever else)

Something like:

<li class="track_name">
    <span class="text">$name1</span>
    <ul>
        <li class="play"><a class="play_link" href="$url">play</a></li>
        <li class="download"><a href="download.php?id=$url">download</a></li>
    </ul>
</li>

$(".track_name").on("click", function(e) {
    $(".selected").removeClass("selected");
    $(this).addClass("selected")
        .find('.play_link').text($(".selected .text").text());
});

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.