0

I have check boxes in the table created dynamically using JSF and i get the HTML output like the following,

 <input name="testForm:j_idt78:0:negative" id="testForm:j_idt78:0:negative" onchange='PrimeFaces.ab({s:this,e:"change",p:"testForm:j_idt78:0:negative"});' type="checkbox">
<input name="testForm:j_idt78:0:positive" id="testForm:j_idt78:0:positive" onchange='PrimeFaces.ab({s:this,e:"change",p:"testForm:j_idt78:0:positive"});' type="checkbox">
<input name="testForm:j_idt78:0:na" id="testForm:j_idt78:0:na" onchange='PrimeFaces.ab({s:this,e:"change",p:"testForm:j_idt78:0:na"});' type="checkbox">

i get the following exception at the console : SCRIPT5022: Syntax error, unrecognized expression: unsupported pseudo: j_idt78

how do i select the checkbox based on the name attribute?

1 Answer 1

1

You'd use the CSS attribute selector:

const inputId = 'testForm:j_idt78:0'
const negativeId = inputId + ':negative'
document.querySelector('input[type="checkbox"][name="' + negativeId + '"]').checked = true
<input name="testForm:j_idt78:0:negative" id="testForm:j_idt78:0:negative" type="checkbox">
<input name="testForm:j_idt78:0:positive" id="testForm:j_idt78:0:positive" type="checkbox">
<input name="testForm:j_idt78:0:na" id="testForm:j_idt78:0:na" type="checkbox">

Sign up to request clarification or add additional context in comments.

3 Comments

i get this exception now "Unable to set property 'checked' of undefined or null reference" , i am trying like the following var negativeId = inputId+":negative"; document.querySelector('input[type="checkbox"][name="+negativeId]').checked = false
It looks like you tried to concatenate the negativeId into the selector string, but you're missing a quote and plus sign. I've edited my answer to use a fixed version of that code.
Thank you so much, you have really saved my day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.