Search code examples
jqueryprototypejs

convert prototype's findElement to jQuery


Can someone explain the jQuery equivalent of Form.event.findElement to me?

$('.ad-publish-button').bind('click', function(e) { // jquery

     // section of prototype I need to convert to jquery
     if ( e.findElement('.ad').down('form').down('#location').value != '' ) {
          e.findElement('.ad').down('form').request();
     }

});

Solution

  • give it a try

    $('.ad-publish-button').bind('click', function(e) { 
        var _form=$('form',$(e.target).closest('.ad')[0]).first();
        if ($('#location',_form).val() != '') {
            $.ajax(_form.prop('action'), {type:_form.prop('method'),data:_form.serialize()});
        }
    });
    
    1. e.target is the clicked element
    2. $(e.target).closest('.ad')[0] is the closest ancestor with className '.ad'
      of the element from #1 . This element is used as context for the selector 'form'
      (so it will find a form inside the .ad-element)
    3. $.ajax() will send a request(using the form- properties action+method and the serialized elements)