Forum: Ruby on Rails changing row color on checkbox click

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Scott K. (Guest)
on 2008-12-30 02:34
say for example i had something simple like this in a view:

<tr class="even"><td><%= check_box_tag(:task) %></td>

how would i add onclick to change the row color on a check and when the
check is removed?

maybe something like this?

page[task].addClassName('complete')

i'm not that familiar with javascript so i'm not sure how i would finish
this.  thanks!
Mark Reginald J. (Guest)
on 2008-12-30 02:59
(Received via mailing list)
Scott K. wrote:
>
> i'm not that familiar with javascript so i'm not sure how i would finish
> this.  thanks!

<%= check_box_tag :task, 1, false,
:onchange => "$(parentNode.parentNode).toggleClassName('complete')" %>

--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
Scott K. (Guest)
on 2008-12-30 03:14
Mark Reginald J. wrote:
> Scott K. wrote:
>>
>> i'm not that familiar with javascript so i'm not sure how i would finish
>> this.  thanks!
>
> <%= check_box_tag :task, 1, false,
> :onchange => "$(parentNode.parentNode).toggleClassName('complete')" %>
>
> --
> Rails Wheels - Find Plugins, List & Sell Plugins -
> http://railswheels.com

perfect!  thank you so much mark!
Scott K. (Guest)
on 2008-12-30 06:17
Scott K. wrote:
> Mark Reginald J. wrote:
>> Scott K. wrote:
>>>
>>> i'm not that familiar with javascript so i'm not sure how i would finish
>>> this.  thanks!
>>
>> <%= check_box_tag :task, 1, false,
>> :onchange => "$(parentNode.parentNode).toggleClassName('complete')" %>
>>
>> --
>> Rails Wheels - Find Plugins, List & Sell Plugins -
>> http://railswheels.com
>
> perfect!  thank you so much mark!

oops spoke a little too soon.  the highlighting works great on firefox 3
but for some reason it acts a little weird on IE7.

the first check box i click doesn't do anything but then if i click
another check box the first check box row gets highlighted.

any idea what could be causing the incompatibility with IE?
Brandon K. (Guest)
on 2008-12-30 06:34
(Received via mailing list)
On Mon, Dec 29, 2008 at 11:17 PM, Scott K.
<removed_email_address@domain.invalid> wrote:
>>>
> another check box the first check box row gets highlighted.
>
> any idea what could be causing the incompatibility with IE?

Try adding `this`:

<%= check_box_tag :task, 1, false,
    :onchange =>
"$(this.parentNode.parentNode).toggleClassName('complete')" %>

--
--------------------------------------------------------------------------------
Training by Collective Idea: Ruby on Rails training in a vacation
setting
http://training.collectiveidea.com – San Antonio, TX – Jan 20-23
Scott K. (Guest)
on 2008-12-30 08:05
Brandon K. wrote:
> On Mon, Dec 29, 2008 at 11:17 PM, Scott K.
> <removed_email_address@domain.invalid> wrote:
>>>>
>> another check box the first check box row gets highlighted.
>>
>> any idea what could be causing the incompatibility with IE?
>
> Try adding `this`:
>
> <%= check_box_tag :task, 1, false,
>     :onchange =>
> "$(this.parentNode.parentNode).toggleClassName('complete')" %>
>
> --
> --------------------------------------------------------------------------------
> Training by Collective Idea: Ruby on Rails training in a vacation
> setting
> http://training.collectiveidea.com � San Antonio, TX � Jan 20-23

same thing :(

i found some info here:

http://dev.rubyonrails.org/ticket/11078

and the link they reference
http://prototypejs.org/learn/extensions...there is a bit at the bottom:

// this will error out in IE:
$('someElement').parentNode.hide()
// to make it cross-browser:
$($('someElement').parentNode).hide()

so....i tried these ways:

$(this.parentNode.parentNode).toggleClassName('complete')

$($(this).parentNode.parentNode).toggleClassName('complete')

$($('make_step18').parentNode.parentNode).toggleClassName('complete')

make_step18 is the id of checkbox i was testing.

but they all produce the same weird result where they don't update until
i click a second time.  first click always does nothing.
Scott K. (Guest)
on 2008-12-30 08:13
Scott K. wrote:
>
> but they all produce the same weird result where they don't update until
> i click a second time.  first click always does nothing.

finally got it.  it was the :onchange that was causing some issues.  i
changed it to :onclick and all methods are now working with IE7.

thanks again everyone!
This topic is locked and can not be replied to.