2 Replies - 883 Views - Last Post: 04 September 2010 - 10:03 AM

#1 huzi8t9   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 374
  • Joined: 11-July 07

.trigger [urgent help needed]

Posted 01 September 2010 - 08:50 AM

Hello, D.I.C heads. I'm hoping you can help me with a slightly nooby bit of code, that I can't seem to get to work.

Here's my stage:
  • $.cookie (plug-in)
  • $('#effects') (checkbox)
  • init_options (function)


Now, take a look at this slightly messy code:
function init_options()
{
	var $init_val = $.cookie('effects') ? $.cookie('effects') : "true";

	$("#effects")
		.attr("checked", $init_val)
                .trigger("change");
}
$(function()
{
	init_options();
	$("#effects").bind("change", function()
	{
		alert("CHANGED")
		$.fx.off = !$("#effects").attr("checked");
		$.cookie('effects', !$("#effects").attr("checked"));
	});
});



When I click my checkbox ($('#effects')), I get the "CHANGED" alert, however when I call the trigger("change") function, I get no alert?

I previously tried to use the trigger function, and I gave up. I could not get it to work, regardless of trying many different methods.

I hope you can help,
Thanks for any help - always appreciated!



-huzi

This post has been edited by huzi8t9: 01 September 2010 - 11:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: .trigger [urgent help needed]

#2 KuroTsuto   User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 182
  • Joined: 13-February 09

Re: .trigger [urgent help needed]

Posted 03 September 2010 - 01:12 PM

Try moving the call to init_options() after the bind call, like so:
function init_options()
{
    var $init_val = $.cookie('effects') ? $.cookie('effects') : "true";

    $("#effects")
        .attr("checked", $init_val)
                .trigger("change");
}
$(function()
{
    $("#effects").bind("change", function()
    {
        alert("CHANGED")
        $.fx.off = !$("#effects").attr("checked");
        $.cookie('effects', !$("#effects").attr("checked"));
    });

    init_options();
});



If it works, don't ask me how... I assume it is a result of triggering the event before the Javascript compiler has bound your custom function to the object, but I can't say for certain.

Cheers,
~KuroTsuto
Was This Post Helpful? 1
  • +
  • -

#3 huzi8t9   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 374
  • Joined: 11-July 07

Re: .trigger [urgent help needed]

Posted 04 September 2010 - 10:03 AM

That was the money!!

Thank-you very much!

-huzi
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1