Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - Find classes with a wildcard

I have multiple classes throughout my page that are like:

<input class="someclass1 condition[operator]" type="text">
<input class="someclass2 condition[value]" type="text">
<input class="condition[percentage]" type="text">

I'd like to use jquery to basically loop through find("some_selector_i_need").each() but I'm not sure how to do this with wildcards.

Is this even possible? Something like find("condition[*]").each() where I could pull out what the * is as well?

I'd eventually like to format an array like:

Array
(
  operator => 'value1',
  value => 'value2',
  percentage => 'value3'
)
like image 552
Geesu Avatar asked Nov 15 '25 22:11

Geesu


1 Answers

var obj = {};

$('[class*="condition"]').each(function(i,ele) {
    var type = ele.className.match(/\[(.*?)\]/)[1];
    obj[type] = 'value' + (i+1);
});

FIDDLE

like image 104
adeneo Avatar answered Nov 17 '25 22:11

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!