1

I have generated json data from the jQuery Querybuilder which I would like to use to attach event listeners automatically but after trying for several hours, I haven't been able to figure out a way.

Here's the querybuilder json of a html form's elements:

  let json = {
    "condition": "OR",
    "rules": [
      {
        "id": "poll_8002",
        "field": "Question 1",
        "type": "integer",
        "input": "radio",
        "operator": "equal",
        "value": 2
      },
      {
        "id": "poll_8002",
        "field": "Question 1",
        "type": "integer",
        "input": "radio",
        "operator": "equal",
        "value": 4
      }
    ],
    "valid": true,
    "then": [
      {
        "id": "field-poll_8002",
        "type": "single",
        "action": "hide"
      }
    ]
  }

Here's the code that I am trying to generate using the json:

  $('input[name="poll_8002"]').on("click change", function () {
    var val = $(this).val()
    if (val === '2' || val === '4') {
      $('.field-poll_8002').hide()
    } else {
      $('.field-poll_8002').show()
    }
  })

Is there any package/script that can help automate this rather than coding with if/else statements? How can I achieve this?

Abdullah Al Nahid
  • 481
  • 1
  • 7
  • 18

0 Answers0