Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi friends,

am working on asp.net gridview
I want to enable and disable texbox in gridview when checkbox is checked in gridview.

it enables when it is checked. and i can enter the value.
When checkbox is disabled clear the value from Textbox

Please help this is my javascript code.

C#
if (!$(this).is(":checked")) {
            var td = $("td", $(this).closest("tr"));
            td.css({ "background-color": "" });
            $("input[type=text]", td).attr("disabled", "disabled");


        } else {
            var td = $("td", $(this).closest("tr"));
            td.css({ "background-color": "#FFFF99" });
            $("input[type=text]", td).removeAttr("disabled");

        }



Thanks in advance.
Posted

If what you means is:
1. When a checkbox is checked, the textbox in its adjacent cell is enabled for entering text.
2. When that checkbox is unchecked, that textbox is clear and disabled.
then, try adapt from this sample:
PHP
<script>
$(document).ready(function(){

    var $checkbox = $("input[type=checkbox]");
    var $textbox = $("input[type=text]");

    $checkbox.prop(
                "checked", false
             );

    $textbox.prop({
                "disabled": true,
                "value": ""
    });

    $checkbox.change(function () {
            var nextTextbox = $(this).closest("td").next().find($textbox);
             nextTextbox.prop({
                "disabled": !$(this).prop("checked"),
                "value": ""
             });
       });
});
</script>
 
Share this answer
 
use below line
JavaScript
$("input[type=text]", td).val('');

complete code will become.

JavaScript
if (!$(this).is(":checked")) {
            var td = $("td", $(this).closest("tr"));
            td.css({ "background-color": "" });
            $("input[type=text]", td).attr("disabled", "disabled");


        } else {
            var td = $("td", $(this).closest("tr"));
            td.css({ "background-color": "#FFFF99" });
            $("input[type=text]", td).removeAttr("disabled");
            $("input[type=text]", td).val('');

        }
</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900