Get invalid HTML code in curly brackets with jQuery
html, javascript, jquery
Solution
So, basically you want to get the 'outer' html and then search for whats in between the curly braces.
The first part of that has been solved here
Get selected element's outer HTML
So using the outerHTML plugin from that 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];
}
Problem
I have some (invalid) HTML code which I cannot change: ``` <a href="#" id="text1"{some-data}>...</a> <a href="#" id="text2"{some-other-data}>...</a> ``` With jQuery, I select one of the two anchors: ``` function someFunction(id) { $('text'+id)...; } ``` Now, I'd like to get the text inside the curly brackets. So, for `id=1` this would mean `some-data`, for `id=2` this would be `some-other-data`. How can I do this? To make it easy: there will be only one curly bracked thing in one element.