...

Get invalid HTML code in braces using jQuery

I have invalid HTML that I cannot change:

<a href="#" id="text1"{some-data}>...</a>
<a href="#" id="text2"{some-other-data}>...</a>

Using jQuery, I select one of two anchors:

function someFunction(id) {
  $('text'+id)...;
}

Now I would like to get the text inside curly braces. So, for id=1it would mean some-data, for id=2it would be some-other-data.

How can i do this?

To make it easy: there will be only one curly brace in one element.

+5
source share
1 answer

So basically you want to get the “external” html and then look for something between the curly braces.

The first part of this issue has been resolved here.

Get HTML code of selected item

So using outerHTML plugin from this question

jQuery.fn.outerHTML = function(s) {
    return s
        ? this.before(s).remove()
        : jQuery("<div>").append(this.eq(0).clone()).html();
};

function someFunction(id) {
  return $('#text'+id).outerHTML().match(/{(.*)}/)[1];
}
+3

All Articles