Reputation: 447
I have a table generated from GridView
defined like that:
<div>
<table cellspacing="0" id="MainContent_GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col"><a href="javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$CustID')">Customer ID</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$CustFirstName')">First Name</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$CustLastName')">Last Name</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$CustCity')">City</a></th><th scope="col">Email</th>
</tr><tr>
<td>
<span id="MainContent_GridView1_lblCustID_0">12</span>
</td><td>
<span id="MainContent_GridView1_lblFirstName_0">Anders</span>
</td><td>
<span id="MainContent_GridView1_lblLastName_0">Rohansen</span>
</td><td>
<span id="MainContent_GridView1_lblCity_0">Takoma Park</span>
</td><td>
<input name="ctl00$MainContent$GridView1$ctl02$txtEmail" type="text" value="[email protected]" id="MainContent_GridView1_txtEmail_0" />
<span data-val-controltovalidate="MainContent_GridView1_txtEmail_0" data-val-errormessage="Must enter Email Address" data-val-validationGroup="grpEmail" id="MainContent_GridView1_ctl00_0" data-val="true" data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid" data-val-initialvalue="" style="visibility:hidden;">Must enter Email Address</span>
<input type="submit" name="ctl00$MainContent$GridView1$ctl02$btnUpdate" value="Update Email" onclick="return ValidateEmail(this);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$GridView1$ctl02$btnUpdate", "", true, "grpEmail", "", false, false))" id="MainContent_GridView1_btnUpdate_0" ButtonType="Button" />
</td>
</tr><tr style="background-color:#EEEEEE;">
<td>
<span id="MainContent_GridView1_lblCustID_1">8</span>
</td><td>
<span id="MainContent_GridView1_lblFirstName_1">Deborah</span>
</td><td>
<span id="MainContent_GridView1_lblLastName_1">Damien</span>
</td><td>
<span id="MainContent_GridView1_lblCity_1">Fresno</span>
</td><td>
<input name="ctl00$MainContent$GridView1$ctl03$txtEmail" type="text" value="[email protected]" id="MainContent_GridView1_txtEmail_1" />
<span data-val-controltovalidate="MainContent_GridView1_txtEmail_1" data-val-errormessage="Must enter Email Address" data-val-validationGroup="grpEmail" id="MainContent_GridView1_ctl00_1" data-val="true" data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid" data-val-initialvalue="" style="visibility:hidden;">Must enter Email Address</span>
<input type="submit" name="ctl00$MainContent$GridView1$ctl03$btnUpdate" value="Update Email" onclick="return ValidateEmail(this);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$GridView1$ctl03$btnUpdate", "", true, "grpEmail", "", false, false))" id="MainContent_GridView1_btnUpdate_1" ButtonType="Button" />
</td>
</tr><tr>
<td>
<span id="MainContent_GridView1_lblCustID_2">7</span>
</td><td>
<span id="MainContent_GridView1_lblFirstName_2">Derek</span>
</td><td>
<span id="MainContent_GridView1_lblLastName_2">Chaddick</span>
</td><td>
<span id="MainContent_GridView1_lblCity_2">Fairfield</span>
</td><td>
<input name="ctl00$MainContent$GridView1$ctl04$txtEmail" type="text" value="[email protected]" id="MainContent_GridView1_txtEmail_2" />
<span data-val-controltovalidate="MainContent_GridView1_txtEmail_2" data-val-errormessage="Must enter Email Address" data-val-validationGroup="grpEmail" id="MainContent_GridView1_ctl00_2" data-val="true" data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid" data-val-initialvalue="" style="visibility:hidden;">Must enter Email Address</span>
<input type="submit" name="ctl00$MainContent$GridView1$ctl04$btnUpdate" value="Update Email" onclick="return ValidateEmail(this);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$GridView1$ctl04$btnUpdate", "", true, "grpEmail", "", false, false))" id="MainContent_GridView1_btnUpdate_2" ButtonType="Button" />
</td>
</tr>
</table>
When validating email field, I want to highlight or set border to red for the email text field. When doing that in my ValidateEmail(btnObj)
function
$(btnObj).siblings('input:name').style.borderColor="red";
I get an error : "Error: Syntax error, unrecognized expression: Syntax error, unrecognized expression: name".
I was able to access text field by (btnObj).siblings('input:text').val()
What is the right way to do that?
Thank's
Upvotes: 1
Views: 1827
Reputation: 55740
$(btnObj).siblings('input:name').
is a jquery Object and you are trying to use Vanilla Javascript method
on it
$(btnObj).siblings('input:name').
should be
$(btnObj).siblings('input[name]')[0]. // Selects inputs with name attribute
Also if you intend to target inputs with the name attribute, your selector has to be changed as to
$(btnObj).siblings('input[name]')
When you are using jQuery why not just use the .css method provided by the library
$(btnObj).siblings('input[name]').css({
borderColor : 'red'
});
Upvotes: 0
Reputation: 277
you can change CSS values with jQuery with the following:
.css("border", "1px solid red")
you could also easily select the required element by its type
$(btnObj).siblings('input[type="text"]').css("border", "1px solid red");
Upvotes: 1