Jquery data() returning undefined

Posted by David Lockwood on Stack Overflow See other posts from Stack Overflow or by David Lockwood
Published on 2012-06-22T03:13:38Z Indexed on 2012/06/22 3:16 UTC
Read the original article Hit count: 185

Filed under:
|

I have been doing a simple jquery data() test. However it is returning undefined. It works on jsfiddle though (http://jsfiddle.net/yrCdj/).

I have a jinja2 loop which populates a div and I want to tag it with some metadata for use later :

{% for s in siteInfo[:markers[0]] %}
<div class="sitebox2">
<script>
$(".sitebox2:last").append('<a href="/sites/d?siteid={{s[5]}}">{{s[0]}}</a><br>Distance                    from {{loc}} is {{s[6]}} km.<br>');
$(".sitebox2:last").data('fruit', 'apple');
</script>

</div>
{% endfor %}

I then try to call this data to test if it is working:

<script type="text/javascript">
$(".sitebox2").each(function(k, v) {
    var f = $(v).data('fruit');
alert(f);
});

</script>

It returns undefined. Any ideas on what is going wrong?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jinja