0

I have a form, an input and a link. If the input has no value, clicking the link would direct to a certain url. If the input is NOT empty, then the click would have to submit the form. This is how the link looks like:

<li class="youtube"><a href="#">YouTube</a></li>

This is what I have so far in Jquery:

$('.youtube').bind('click', function() {
    if($(input).val() == ''){
        $(".youtube a").attr("href", "http://www.youtube.com/")
    }else{
      $(form).attr("action","http://www.youtube.com/results?search_query=");
      $(form).submit();
    }

});

This doesn't work, I can't figure out why.

Youss
  • 4,196
  • 12
  • 55
  • 109

1 Answers1

2

You want to be targetting the link rather than the li

I.E.

$('.youtube a').bind('click') etc..

Also have you got multiple input fields? Your input field needs to be in quotes otherwise you're targetting a variable, i.e. 'input'

To check for an empty string i'd just do:

if(!$('input').val())
{
    // Rest of code here
}

So your full code would be:

$('.youtube a').bind('click', function() {
    if(!$('input').val()){
        $(".youtube a").attr("href", "http://www.youtube.com/")
    }else{
      $('form').attr("action","http://www.youtube.com/results?search_query=");
      $('form').submit();
    }

});
Community
  • 1
  • 1
christian.thomas
  • 1,122
  • 1
  • 8
  • 19
  • Sorry, I didn't notice that earlier.. It's the same case as with the input field, you need to put quotes round the `$(form)` to make it `$('form')` otherwise you'll again be targetting the variable 'form' - I've corrected my answer to reflect the correct solution. – christian.thomas May 18 '12 at 17:21