Reputation: 2560
I am trying to select all the anchors contained in a variable that is html as below my code:
var deposti_row = "
<a data-country='uk' data-currency='eur' data-matching='country-currency'> AA </a>
<a data-country='uk' data-currency='eur' data-matching='country'> BB </a>
<a data-country='uk' data-currency='eur' data-matching='none'> CC </a>
"
$("a['data-matching']",$(deposit_row)).each( function(){
console.log(this);
});
But i get the error Syntax error, unrecognized expression: a['data-matching'] . Any help please ?
Upvotes: 0
Views: 623
Reputation: 68393
You need to remove single quotes from the attribute selector, then it should work as long anchor tag a
has a parent element.
You need to wrap them in a div
to treat them as descendants
deposit_row = $(deposit_row).wrap("<div></div>").html();
$("a[data-matching]",$(deposit_row)).each( function(){
console.log(this);
});
Also try this if a['data-matching']
is the child of a root node
$(deposit_row).find("a['data-matching']").each(function(){
});
But since a
is the root node here
$(deposit_row).children("a['data-matching']").each(function(){
});
Upvotes: 0
Reputation: 15154
Heres another way:-
\
.has
inside your each.var deposit_row = "\
<a data-country='uk' data-currency='eur' data-matching='country-currency'> AA </a>\
<a data-country='uk' data-currency='eur' data-matching='country'> BB </a>\
<a data-country='uk' data-currency='eur' data-matching='none'> CC </a>\
";
$(deposit_row).each(function() {
if ($(this).has('[data-matching]'))
console.log(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1
Reputation: 74420
Your syntax is wrong. Anyway, what you really want is to filter matched set:
$(deposit_row).filter('a[data-matching]').each(...)
And your string syntax is wrong too regarding handling mutliple lines.
And deposti_row
is not the same as deposit_row
.
That makes many things wrong is so few posted code...
Upvotes: 2