-1

With alert debug, the branch of if was executed. But the code tag that matched did not get the "data-language" attribute.

divs.html(function (index, oldhtml) {
    var newhtml = converter.makeHtml(oldhtml);
    var pat = /\{-# CODE (.+?) #-}/i;
    $('code', newhtml).text(function (index, text) {
        var m = text.split("\n")[0].match(pat);
        if (m) {
            $(this).attr("data-language", m[1]);
        }
    });
    return newhtml;
});
4
  • 2
    Did you check what the value of m[1] is?
    – JJJ
    May 17, 2013 at 10:00
  • 1
    It is because you are not modifying newhtml string with the attribute, you are creating a dom copy of newhtml and modifying it, those changes are not brought back to newhtml hence it is not working May 17, 2013 at 10:06
  • @Juhana : Yes. With test text, I got in the branch of if (m) .
    – Magicloud
    May 17, 2013 at 13:18
  • @ArunRJohny : I suspected that. But I do not know how fix that....
    – Magicloud
    May 17, 2013 at 13:20

1 Answer 1

0

But the code tag that matched did not get the "data-language" attribute.

That's because you're not getting the "data-language" attribute, you're setting it.

$(this).attr("data-language", m[1]);

This sets the "data-language" attribute to whatever is stored in m[1]. Please refer to jQuery's .attr() documentation.

To get the data from the element, you'd use either:

$(this).attr('data-language');

Or (more semantically correct):

$(this).data('language');

To set data attributes, you should again make use of jQuery's .data() method:

$(this).data('language', m[1]);
1
  • 1
    I think by "the code tag that matched did not get the "data-language" attribute." they mean the attribute isn't set on that element. May 17, 2013 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.