Richard77
Richard77

Reputation: 21671

jQuery is not displaying the value of a textbox located in a user control

I have this textbox located inside a gridview which is located in a user control. I'm trying to check whether a textbox is empty, using the below jQuery code.

$(document).ready(function () {//when Dom is ready to be used
   $('input[type=submit]').click(function () {
    CheckIfCheckBoxIsChecked(this); //Use the clicked button to proceed
   });
 });

function CheckIfCheckBoxIsChecked(e) {
   var $id = $(e).prop('id'); //get the id of the clicked button
   var $gvid = "";

//Set the name of the gridview
if ($id.indexOf('IT') != -1) {
    $gvid = "contentMain_cklistIT_gvCKList";
}
else if ($id.indexOf('HR') != -1) {
    $gvid = "contentMain_cklistHR_gvCKList";
}
else if ($id.indexOf('Marketing') != -1) {
    $gvid = "contentMain_cklistMarketing_gvCKList";
}
else if ($id.indexOf('Payroll') != -1) {
    $gvid = "contentMain_cklistPayroll_gvCKList";
}
else if ($id.indexOf('Property') != -1) {
    $gvid = "contentMain_cklistProperty_gvCKList";
}

var $AllHM = $('#' + $gvid + ' input[id*="ckHMreq"]:checkbox:checked'); //select only checked checkboxes that have ckHMreq as part of their name 
var n = $AllHM.length;
if (n == 0) {//If there's none, telle the user
    alert('Please check at least one row to proceed');
}
else {//If there's at least one
    $.each($AllHM, function () {//Loop through each of them
        cid = $(this).prop('id');
        var nrow = cid.match('\\d+'); //find the row number

        var ckDpt = $('#' + $gvid + ' input[id*="ckDpt_' + nrow + '"]:checkbox'); //select the appropriate checkbox
        if ($(ckDpt).prop('checked') == true) {// if the checkbox is selected
            var a = $('#contentMain_cklistHR_gvCKList_txtCompletedBy_' + nrow);
            if (a.val() == "") {
                alert('Please enter your name to proceed');
                //more to come...
            }
        }
    });        
}

}

The problem is when I type something, the alert message box still pops up. The only time the alert don't pops up is when the textbox displays text from the server (.i.e. when the page loads).

Is there any reason for that?

EDIT

This HTML is from the browser:

<input name="_ctl0:contentMain:cklistIT:gvCKList:_ctl2:txtCompletedBy" type="text" id="contentMain_cklistIT_gvCKList_txtCompletedBy_0" />

They are all the same except that the last character is 0, 1, .. to 5

Upvotes: 0

Views: 186

Answers (1)

Jeff S
Jeff S

Reputation: 7484

The is of the actual textbox doesn't match the value jQuery is using to search.

The id of the actual textbox is:

id="contentMain_cklistIT_gvCKList_txtCompletedBy_0"

But the id being checked is:

'#contentMain_cklistHR_gvCKList_txtCompletedBy_' + nrow

(The chklist portions are different - one is HR, one is IT).

Upvotes: 2

Related Questions