skip to Main Content

I am new to jQuery, I need to get the element value from this input field

<input type="number" onkeypress="preventSomeKeys(event)" onchange="setTwoNumberDecimal(this)" name="fee_ctv_omp_percent" id="fee_ctv['omp_percent']" value="0.00" class="w-100" step="0.01" style="border:none;">

as you can see the id contains special chars.

I write this code,

var test2 = $("#fee_ctv['omp_percent']");

and this is the results:

Uncaught Error: Syntax error, unrecognized expression: #fee_ctv['omp_percent']

enter image description here

Is their a way to get the element by id in this scenario?

2

Answers


  1. Try with:

    var test2 = $("#fee_ctv\['omp_percent\']");
    

    For more info check https://api.jquery.com/id-selector/

    Login or Signup to reply.
  2. You can use the escape selector:
    $.escapeSelector("fee_ctv['omp_percent']")

    and put it in your js like this:

    var test2 = $(`#${$.escapeSelector("fee_ctv['omp_percent']")}`);
    

    working codepen

    This way, you don’t have to manually escape every entry.
    link to docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search