WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this array for checkbox:

        'name' => __('Latest Product',''),
        'desc' => __('Select if you want to display latest product',''),
        'id' => $prefix . 'checkbox',
        'type' => 'checkbox',
        'std' => ''

I want to add an attribute for onclick="chk(electronics, this.checked);" on this.

How can I do it?

share|improve this question
In which context do you have this checkbox? – fischi Jan 2 '13 at 11:23
Your question is incomplete, provide complete code. – sven Jan 2 '14 at 5:40

Store the ID in a variable, and access that variable using JavaScript:

document.getElementById("<?php echo $checkbox_id; ?>").onclick = function() {
    chk(electronics, this.checked);

You can also collect multiple IDs in an array and run through that array later in your script. But avoid the onclick attribute in markup, this is bloat and bad code style. :)

share|improve this answer
Thanks for giving answer... – Ashok Saini Jan 3 '13 at 4:58
it's not working in my file..... can i inlcude this line <input type="checkbox" id="checkbox" onclick="chk(electronics, this.checked);"> in above array..... – Ashok Saini Jan 3 '13 at 5:17
You could add a separate key named onclick and use it in the script that converts the array into markup. You didn’t mention that script, so I cannot say how this works. – toscho Jan 3 '13 at 14:17

If you want to go with JQuery then use below code example.


//Temporary set electronics variable with blah, replace it with your code
var electronics = 'blah...';

//For set onclick attribute after page load
        $(this).attr('onclick', 'chk(electronics, this.checked);');

For live example please check my JSFIDDLE. In this fiddle you can check functionality by click on any check box, it will prompt message with checked true/false...

This is based on your question, if it not as you want then please give some more detail what you want.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.