Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  146] [ 2]  / answers: 1 / hits: 18836  / 13 Years ago, wed, november 16, 2011, 12:00:00

I have a javascript file that Select a button using jQuery and check it for validation.the code is:



$(document).ready(function () {
$('#<%= btn_Insert.ClientID %>').on(click, function (event) {

if (Validate() != true) {
event.preventDefault();
}
});
function Validate() {
return (1 == 1);
});


but I can' get ClientID for button and I get this Error:




Object doesn't support this property or method




where is my mistake?How I can get Client ID for a server side Button?



thanks



EDIT 1)



When I add script in head part of page it works but when I write it to a JS file and add reference to it in head part it does not work


More From » jquery

 Answers
45

I suspect that the error comes from the fact that event has a special meaning in some browsers. Try renaming the variable:



function Validate() {
return (1 === 1);
}

$(function () {
$('#<%= btn_Insert.ClientID %>').on('click', function (evt) {
if (!Validate()) {
evt.preventDefault();
}
});
});


or even easier :



$('#<%= btn_Insert.ClientID %>').on('click', function() {
return Validate();
});





UPDATE:



Now that you have edited your question it is clear where the problem is. You are trying to use server side tags (<%= %>) in a separate javascript file. That's impossible. They will render literally and will not be processed by ASP.NET.



In order to solve your issue you could apply a CSS class to the button and then use a class selector:



$('.someClass').on('click', function() {
...
});


Another possibility is to use the ends with selector:



$('[id$=btn_Insert]').on('click', function() {
...
});

[#89088] Tuesday, November 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mary

Total Points: 432
Total Questions: 98
Total Answers: 98

Location: Luxembourg
Member since Tue, Jan 25, 2022
2 Years ago
;