Stephen
Stephen

Reputation: 1148

Matching 2 specific regex in jquery

Maybe there's no solution for this.

$("input[id^='pricechange']").on("input", function() {
   if (this.value != $("input[id^='rate']")) {

   }
});

I have a table that has inputs in the cell, it's dynamic and can have any amount of rows. I'm using regex to pick up any changes to a field: pricechange123, pricechange000, pricechange999, etc. Once a user inputs a value it needs to cross reference it with it's accompanying value in the rates column: rate123, rate000, and rate999. As of now $("input[id^='rate']") would pick up all the rates.

Is there a way to tell which id was picked up?

Upvotes: 0

Views: 28

Answers (1)

Daniel Beck
Daniel Beck

Reputation: 21514

Inside that input handler, it's just this.id.

$("input[id^='pricechange']").on("input", function() {
  console.log(this.id);

  // extract the numeric part:
  var myIdNumber = this.id.replace('pricechange', '');
  // so you can use it on the #rate123 element similarly:
  if (this.value != $('#rate' + myIdNumber).value) {
    // ...
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="pricechange1">
<input id="pricechange2">

Upvotes: 1

Related Questions