6

I want to grey out these two things the song input field and the submit button until the user enters and artist. Is there an easy way to do this via JQuery.

the id of the artist input field is #request_artist

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321

3 Answers3

12

You can do something like this:

var artist = ('#request_artist');
var song = ('#request_song');
var assubmit = ('#request_submit');

song.attr('disabled', true);
assubmit.attr('disabled', true);

artist.change(function() {
  if(artist.val() > 0) {
    song.attr('disabled', false);
    assubmit.attr('disabled', false);
  } else {
    song.attr('disabled', true);
    assubmit.attr('disabled', true);
  }
});
xil3
  • 16,305
  • 8
  • 63
  • 97
4

for the input field, the submit button should be equal $('#request_artist').attr('disabled', true);

ipsum
  • 1,042
  • 7
  • 17
3

The one liner code would be :

     <input type="text" name="name" value="" id="txt1" />
     <input type="button" name="name" id="btn1" disabled="disabled" value="Submit" />

<script type="text/javascript">

            $("#txt1").keyup(function () {
                $("#btn1").attr("disabled", $.trim($("#txt1").val()) != "" ? "" : "disabled");
            });


</script>
Siva Gopal
  • 3,474
  • 1
  • 25
  • 22
  • 1
    It's not code golf here, why do you *have* to squash your code onto a single line? And how is having fewer lines make this solution superior? – Yi Jiang Sep 19 '10 at 15:04
  • I am sorry, my intention is to make use of Jquery Chaining feature. – Siva Gopal Sep 19 '10 at 15:08