February 26, 2012

Why can't I enable and uncheck an element with jQuery?

Question by just_name

I want to enable and set check = false by editing this code?

if (confirm('This widget will be removed, ok?')) {
    $(​'table tr input[type=hidden]').filter(function() {
        return $(this).val()​​​​​ == widgetId;
    }).siblings('input[type=checkbox]').attr({disabled: true, checked: true});

I try :

.siblings('input[type=checkbox]').removeAttr('disabled');

and this.

.siblings('input[type=checkbox]').attr({disabled:false, checked: false});

but in vain


EDIT 1:

<table><colgroup><col title="process name"></colgroup> <tbody><tr><td><input name="rlv_mainservices$ctrl0$hf_id" id="rlv_mainservices_ctrl0_hf_id" value="91" type="hidden"> <input id="rlv_mainservices_ctrl0_chb_sys" name="rlv_mainservices$ctrl0$chb_sys" type="checkbox"> <span id="rlv_mainservices_ctrl0_lbl_main_sys">pro1</span> </td></tr></tbody></table><table><colgroup><col title="processname"> </colgroup><tbody><tr><td><input name="rlv_mainservices$ctrl1$hf_id" id="rlv_mainservices_ctrl1_hf_id" value="92" type="hidden"><input id="rlv_mainservices_ctrl1_chb_sys" name="rlv_mainservices$ctrl1$chb_sys" type="checkbox"> <span id="rlv_mainservices_ctrl1_lbl_main_sys">pro2</span> </td></tr></tbody></table>

I wanna to enable input type=”checkbox” if the value of the input type =”hidden” that exist in the same <td> equal to the id of li(widget).This when the user click close.

Answer by Starx

Actually you can do this with jQuery.

To enable

$('#element').removeAttr('disabled');

To uncheck

$('#element').removeAttr('checked');

Or, even Combine them

$('#element').removeAttr('disabled').removeAttr('checked');

Just in case, you manipulated the attribute after it has been loaded, then it will no longer be available as an attribute from jQuery 1.6.

If you are using so and later, try .removeProp( propertyName ) [docs here] instead.

$('#element').removeProp('disabled').removeProp('checked');

Update

  • Update 1: It does work. Check a demo here.

  • Update 2: I have also simulated the manipulation and created another fiddle using .removeProp(), check it here

  • Update 3: Ok, check this update. It it compatible with your markup, however, you might have to use proper selector, for you. However, this will give you the entire idea.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!