Cheknov
Cheknov

Reputation: 2082

How to compare the value of two selects in Javascript with dynamic content using javascript

I currently have these two conditions that must be met for a modal to display on screen:

if($("#id_form-0-calculated").val() === $("#id_form-0-modified").val()) {
  if($("#id_form-0-calculated").val() === $("#id_form-0-modified").val()) {
    $("#Modal").modal();
  }
}

This works perfectly for the first element (N = 0), the problem is that the web page I am editing can have N elements with the following pattern:

id_form-N-calculated / id_form-N-modified

How could I make the if statements dynamic and work for all existing N values instead of just one static value?

Upvotes: 0

Views: 101

Answers (2)

Epic Chen
Epic Chen

Reputation: 1372

You could write a function something like

function check(n){
    if($("#id_form-" + n + "-calculated_ad_pathogenicity").val() === $("#id_form-" + n + "-modified_ad_pathogenicity").val()) {
      if($("#id_form-" + n + "-calculated_ar_pathogenicity").val() === $("#id_form-" + n + "-modified_ar_pathogenicity").val()) {
        $("#Modal").modal();
      }
    }
}

Adding it to the checking flow. For example

const n = 4;

for(let i = 0; i < n; i++){
     check(i);
}

Upvotes: 0

aprl11
aprl11

Reputation: 53

I don't know how will you do this, but I regularly use the following:

var N = 0;

if($("#id_form-"+N+"-calculated_ad_pathogenicity").val() === $("#id_form-"+N+"-modified_ad_pathogenicity").val()) {
   if($("#id_form-"+N+"-calculated_ar_pathogenicity").val() === $("#id_form-"+N+"-modified_ar_pathogenicity").val()) {
      $("#Modal").modal();
   }
}

Most of the time, N is used in either loops or functions.

Upvotes: 1

Related Questions