0

I've got a problem that's been racking my brain since I'm rather inexperienced with programming. I'm a member of a website that hosts image galleries. When you view a page with a list of galleries, every gallery has tags underneath it inside of icons.

<div class="id4">
    <a href="..."><img src="..."></a>
    <div class="id44">
        <div class="tft" title="tag1, tag2"></div>
        <div class="tft" title="tag3"></div>
    </div>
    <div class="tags"></div>
</div>
<div class="id4">
    <a href="..."><img src="..."></a>
    <div class="id44">
        <div class="tft" title="tag1"></div>
        <div class="tft" title="tag2"></div>
    </div>
    <div class="tags"></div>
</div>

Since hovering over each icon is a big hassle, I wanted to write a custom script that writes the tags inside of the "title" attribute underneath the gallery they belong to. This is as far as I could get:

$(".id44").after('<div class="tags"></div>');
$(".id44").each(function() {
    var array = [];
    $(".tft").each(function() {
        var tags = $(this).attr("title");
        array.push(tags);
    });
    console.log(array);
});

All it does is print a gigantic list of every single tag on the page to the console as many times as there are galleries.

2 Answers 2

1

Look for the .tft elements that are descendants of the current element:

$(this).find(".tft").each(function() {
    array.push(this.title);
});

$(".tft") alone matches all elements with a class of tft.

I would write the whole thing like this:

$('.id44').each(function() {
    var tags = $(this).find('.tft').map(function() {
        return this.title;
    }).get();

    $('<div>', {
        'class': 'tags',
        'text': tags.join(', ')
    }).insertAfter(this);
});

.map(...).get() is just a shorter way of writing the code that makes an array of all the tags.

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

2 Comments

This functions perfectly, thank you. I have one more small question regarding the tags. Some of them have namespaces attached to them (eg: "namespaceX:tag1" "namespaceY:tag2") How can I strip those from the output?
@user1689634: Just do this.title.split(':').slice(-1)
1

The problem is that .tft is not limited to the current .id44 you are looping over. Using "selector", this will limit the result to the this-container. In addition, I've added how to add the tags in the right .tag-div.

$(".id44").after('<div class="tags"></div>');
$(".id44").each(function() {
    var array = [];
    $(".tft", this).each(function() {
        var tags = $(this).attr("title");
        array.push(tags);
    });
    $(".tags", this).html(array.join(','));
});

1 Comment

I didn't know "selector", this was possible, thank you. That helps a lot.

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.