Here is my statement:
$(document).ready(function() {
$('#search_option').change(function() {
alert('changed');
if( $('#search_option').val() == "wiki" ) {
$('#search_form').setAttribute('action', "http://www.wikipedia.org/search-redirect.php");
$('#search_bar').setAttribute('name', "search");
alert('Got inside wiki');
} else {
$('#search_form').setAttribute('action', "http://www.google.com/search");
$('#search_bar').setAttribute('name', "q");
alert('Got inside google');
}
});
});
Neither of the 'got inside' alerts are triggering, meaning that neither of them are running, correct? I can not seem to figure out why neither parts of the if statement are running, at least one should be
setAttribute
is a dom element node method use .attr()
thats the jQuery method
$(document).ready(function() {
$('#search_option').change(function() {
alert('changed');
if( $('#search_option').val() == "wiki" ) {
$('#search_form').attr('action', "http://www.wikipedia.org/search-redirect.php");
$('#search_bar').attr('name', "search");
alert('Got inside wiki');
} else {
$('#search_form').attr('action', "http://www.google.com/search");
$('#search_bar').attr('name', "q");
alert('Got inside google');
}
});
});
Since setAttribute
is not a method of the jQuery object an error is produced and the code stops execution therefore it never reaches the alerts.
.setAttribute()
isn't a valid jQuery method. Use .attr()
.
$(document).ready(function() {
$('#search_option').change(function() {
alert('changed');
if( $('#search_option').val() == "wiki" ) {
$('#search_form').attr('action', "http://www.wikipedia.org/search-redirect.php");
$('#search_bar').attr('name', "search");
alert('Got inside wiki');
} else {
$('#search_form').attr('action', "http://www.google.com/search");
$('#search_bar').attr('name', "q");
alert('Got inside google');
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With