try the following: In your case, javascript stops after entering if or else, and then throws an error before it reaches the warning statement, to check similar cases, you can move the warning statement line by line down. check for breakpoints with Firebug or other devTools for a better solution
$(document).ready(function() { $('#search_option').change(function() { alert('changed'); if( $('#search_option').val() == "wiki" ) { alert('Got inside wiki'); $('#search_form').setAttribute('action', "http://www.wikipedia.org/search-redirect.php"); $('#search_bar').setAttribute('name', "search"); } else { alert('Got inside google'); $('#search_form').setAttribute('action', "http://www.google.com/search"); $('#search_bar').setAttribute('name', "q"); } }); });
change to jQuery attr() instead of setAttribute since $('#something') is a jQuery object and not a DOM node to fix the error.
final code:
$(document).ready(function() { $('#search_option').change(function() { alert('changed'); if( $('#search_option').val() == "wiki" ) { alert('Got inside wiki'); $('#search_form').attr('action', "http://www.wikipedia.org/search-redirect.php"); $('#search_bar').attr('name', "search"); } else { alert('Got inside google'); $('#search_form').attr('action', "http://www.google.com/search"); $('#search_bar').attr('name', "q"); } }); });
source share