0

I have a jquery code but is not working and seems that I need prototype code.

Here is the code: http://jsfiddle.net/qKG5F/1627/

<script type="text/javascript">
(function() {
$('form > input').keyup(function() {

    var empty = false;
    $('form > input').each(function() {
        if ($(this).val() == '') {
            empty = true;
        }
    });

    if (empty) {
        $('#search').attr('disabled', 'disabled');
    } else {
        $('#search').removeAttr('disabled');
    }
});
})()
</script>
<form>
FROM<br />
<input type="text"/><br />
TO<br />
<input type="text"/><br />     
<input type="submit" id="search" value="GO" disabled="disabled" />
</form> 

Please somebody can help me to convert this jquery to prototype code?

All kind of help will be accepted.

5
  • that code works , select onDomready from dropdown instead of onLoad in jsfiddle: see:: jsfiddle.net/qKG5F/1628 Commented Apr 15, 2014 at 4:08
  • yes but it is a jquery code a friend told me to convert this code to prototype code Commented Apr 15, 2014 at 4:11
  • jsfiddle.net/qKG5F/1628 Commented Apr 15, 2014 at 4:12
  • @CarlitosMorales: a) why convert it? There should be a better reason than "someone said so". b) What have you tried? Do you know Prototype at all? StackOverflow is not a coding service. Commented Apr 15, 2014 at 7:05
  • I know is not code service, i just was asking, well a friend told me that there is a difference using protoype, well according to here the error is not prototype, the error is mine because i'm using anothers javascript files. This question is done ....thanks for helping Commented Apr 15, 2014 at 13:17

2 Answers 2

3

For sake of completeness I went ahead and converted your code to PrototypeJS. I optimized the code a bit (sorry can't help it) to exit when the first empty field is found.

<script type="text/javascript">
document.observe('dom:loaded',function(){
    $$('form > input').invoke('observe','keyup',function() {
        var empty = false;
        $$('form > input').each(function() {
            if (this.value == '') {
                empty = true;
                throw $break;
            }
        });
        $('search').writeAttribute('disabled',empty);
    });
});
</script>
<form>
FROM<br />
<input type="text"/><br />
TO<br />
<input type="text"/><br />     
<input type="submit" id="search" value="GO" disabled="disabled" />
</form> 
Sign up to request clarification or add additional context in comments.

1 Comment

oh great your code worked perfect, thanks Geek , the problem was that i used lots javascript files and was doing conflict and a friend suggest me convert this jquery code to prototype, and then used your code and worked perfect 10 of 10 without conflicts
1

The issue is that your JavaScript is executing before the DOM elements load.

A very simple fix would be to wrap your function invocation within the default jQuery function.

<script type="text/javascript">
$(function() {
  $('form > input').keyup(function() {

      var empty = false;
      $('form > input').each(function() {
          if ($(this).val() == '') {
              empty = true;
          }
      });

      if (empty) {
          $('#search').attr('disabled', 'disabled');
      } else {
          $('#search').removeAttr('disabled');
      }
  });
});
</script>
<form>
FROM<br />
<input type="text"/><br />
TO<br />
<input type="text"/><br />     
<input type="submit" id="search" value="GO" disabled="disabled" />
</form> 

Passing a function to jQuery is the equivalent of calling $(document).ready(someFunctionHere). What this does is defer the execution of the function until the DOM has finished loading.

Alternatively, putting your script tag at the bottom of your HTML would also achieve the desired effect.

1 Comment

Thanks friend your code was working fine. I appreciate your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.