Neither one nor the other starts

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'); } }); }); 

None of the โ€œinsideโ€ warnings are triggered, which means that none of them work, right? I canโ€™t understand why none of the parts of the if statement work, at least one should be

+4
source share
5 answers

.setAttribute() not 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'); } }); });โ€‹ 
+3
source

setAttribute is a dom node use .attr() element, it is a 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 not a jQuery object method, an error occurs and the code stops executing, so it never reaches warnings.

+9
source

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"); } }); }); 
+1
source

Change the line here

 if( $('#search_option').val() == "wiki" ) { 

to

 if($(this).find('option:selected').val() == "wiki") { 
0
source

to try:

 if($('#search_option option:selected').val()=="wiki"){ 
0
source

All Articles