Also remove the onclick
handler from HTML because you are already attaching an event using jQuery/javaScript -
<button type="submit" value="" onclick="return empty();" id="btn-search"/>
change to -
<button type="submit" value="" id="btn-search"/>
Return the result of calling the empty()
function;
window.addEvent('domready', function () {
var btnsearch = $('#btnsearch'); // Added #
btnsearch.addEventListener('click', function (event) {
return empty(); // Changed here...
});
function empty() {
var searchInput = $('pal');
alert(searchInput.get('text'));
if (searchInput.get('text') == "") {
// Show some error to the user,
return false;
}
return true; // Changed here...
}
});
If you are using jQuery, you should probably write this code instead --
$(function(){
$('#btnSearch').click(function(){
return empty();
})
function empty() {
var searchInput = $('pal');
alert(searchInput.get('text'));
if (searchInput.get('text') == "") {
// Show some error to the user,
return false;
}
return true;
}
})
EDIT -
As suggested by @Tom, I agree that adding it to the form's submit event is always a better choice. Please try this code -
$(function(){
$('#home-search-form-form').submit(function(){
return empty();
});
function empty() {
var searchInput = $('pal');
alert(searchInput.get('text'));
if (searchInput.get('text') == "") {
// Show some error to the user,
return false;
}
return true;
}
})