0

Having real trouble with this one and I know it's simple. Google isn't helping too much because I'm not quite sure what to ask. So here's the deal:

I've got a list of anchors and need to append two attributes from each of them to an array in a specific format. I can easily grab the attributes for each element, but I'm not sure how to append them to the array. Here's what I'm looking for:

Goal Array Output

slides = [{
    img: 'img/aaron1.jpg',
    desc: '127'
}

HTML

<section id="photoBin">
    <ul>
        <li><a id="test" href="img/aaron1.jpg" title="Photo #127">127</a></li>
        <li><a href="img/aaron2.jpg" title="Photo #128">128</a></li>
        <li><a href="img/aaron3.jpg" title="Photo #129">129</a></li>
        <li><a href="img/aaron4.jpg" title="Photo #130">130</a></li>
        <li><a href="img/aaron5.jpg" title="Photo #131">131</a></li>
        <li><a href="img/aaron6.jpg" title="Photo #132">132</a></li>
    </ul>
</section>

JavaScript

var slides = [];

$('li a').each(function(){

    var el = $(this)
        img,
        desc;

        img = el.attr('href');
        desc = el.attr('title').split('Photo #');
});

Thanks for your help! Lets chalk this one up to easy reputation :)

5 Answers 5

3

You're missing the part where you add them to the array via push.

Also, split gives you an array; if you're only looking for the second piece of the split (the number), you need to access the second element of that array via the index accessor [1]:

http://jsfiddle.net/H4wWX/1/

var slides = [];

$('li a').each(function(){
    var el = $(this);

    slides.push({
        img: el.attr('href'),
        desc: el.attr('title').split('Photo #')[1]
    });
});

You may also want to consider bullet-proofing this; if el.attr('title') is not a string or doesn't contain "Photo #", the split/index accessor will throw an exception.

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

3 Comments

The title will theoretically always have the prefix, but i prefer reducing the chances of error. do you have a recommendation for always picking out the number string from the title? it will for sure always include a number, though the digit length might vary
also huge kudos for the reduced variables. i couldn't for the life of me remember the .push() method- so additional kudos for not ridiculing!
The only possibly-cleaner way would be if he got his number from the el.text() rather than splitting on the title attribute, afaik, but that requires that his a-tag's text always be the number. On the other hand, the split method relies on his title never changing, either, so it's a bit fragile, either way.
0
var slides = [];

$('li a').each(function(){

    var el = $(this)
        img,
        desc;

        img = el.attr('href');
        desc = el.attr('title').split('Photo #');
slides.push({
     img: img ,
    desc: desc 
})
});

6 Comments

do you only need the first one?
"desc" will end up being an array with two values... desc[0] will be '', and desc[1] will be the number.
so just take the value that you need ?!?
and why not take the text value?
I'm just saying that your method will end up having an array for the desc value, which is not what he's looking for. He needs to take, if he's using your example, desc[1] if he wants just the numeric part.
|
0

Small modification should get you what you're looking for:

var slides = [];

$('li a').each(function(){

    var el = $(this)
        img,
        desc;

    var slide = { img : el.attr('href'),
                  desc: el.attr('title').split('Photo #')[1] };
    slides.push(slide); 
});

It should be noted that this is not necessarily foolproof. Look forward to odd js errors if the title attribute is ever wrong or empty. You might want to do some testing to make sure you're getting what you expect, before pushing the slide to the array.

Comments

0

Fiddle: http://jsfiddle.net/gromer/zqn43/

var slides = [];

$('li a').each(function(){

    var el = $(this);
    var imgHref= el.attr('href');
    var description = el.attr('title').split('Photo #')[1];

    slides.push({
        img: imgHref,
        desc: description
    });
});

Comments

0

you want to add the following line, right?

slides.push({img: img, desc: desc}) 

It's too simple for a question, so may be you need something different?

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.