Jonah Katz
Jonah Katz

Reputation: 5288

get jquery script to work with prototype?

Him

Im trying to convert my jQuery script to a Prototype compatible one, but i cant seem to get it to work. Can someone please help me? Basically what it does is allow you to click an entire html table row to check/uncheck a checkbox.

$('input:checkbox').hide()
$('.clickable tr').click(function() {
var $checkbox = $(this).find(':checkbox').filter(':first');
$checkbox.attr('checked', !$checkbox.attr('checked'));
$(this).toggleClass('selected');
});

Upvotes: 0

Views: 107

Answers (3)

Robin Maben
Robin Maben

Reputation: 23054

EDIT: If you are looking to for dual compatibility of js libraries consider using the $.noConflict() and use jQuery everywhere instead of $.

If you are looking to toggle a checkbox value simply by clicking the row, below is how you would do it in jQuery. here, is the working example.

$(function(){
    $('tr').click(function(){
        var checkbox = $(this).find('input[type="checkbox"]').first();
        if(checkbox.attr("checked")){
            checkbox.removeAttr("checked");
        }
        else{
            checkbox.attr("checked", "checked");
        }
    });
});

Upvotes: 0

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99921

In Prototype you use $$() to select elements with css-like selectors.

This returns an Array of Elements. Then you could use the .each() method on the returned value:

$$('input:checkbox').each(function(e) { e.hide(); });

For events, you use the Element.observe() method:

$$('.clickable tr').each(function(e) {
    e.observe(function(event) {
        var checkbox = $(this).select('[type="checkbox"]:first');
        checkbox.checked = !checkbox.checked;
        $(this).addClassName('selected');
    });
});

(untested)

Upvotes: 0

kleinohad
kleinohad

Reputation: 5912

use the word jQuery instead of $ also read this: http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Upvotes: 1

Related Questions