Reputation: 703
I want trigger my click event on my menu1 on document load function. As i have many function on that menu i assigned it to a variable. And called the click event on that variable. But its not working.
$(document).ready(function () {
var $menuone = $('#menu1');
var $menutwo = $('#menu2');
var $menuthree = $('#menu3');
var $menufour = $('#menu4');
$menuone.trigger("click");
$("#menuone").click(function () {
$("#frm1").show();
$("#frm3").hide();
$("#frm4").hide();
$("#frm2").hide();
$(this).css({
border: "2px solid #A69A8F",
"border-bottom-width": "0",
background: "#F7F7F7",
color: "#0D638B;"
});
$(this).children().css({
color: "#0D638B",
"font-weight": "bold"
});
});
$menutwo.click(function () {
$menuone.removeAttr('style');
$menuone.children().removeAttr('style');
$("#frm1").hide();
$("#frm3").hide();
$("#frm4").hide();
$("#frm2").show();
$(this).css({
border: "2px solid #A69A8F",
"border-bottom-width": "0",
background: "#F7F7F7",
color: "#0D638B;"
});
$(this).children().css({
color: "#0D638B",
"font-weight": "bold"
});
});
});
Upvotes: 0
Views: 59
Reputation: 1697
Browsers run your JavaScript line by line. Therefore, your binding of $("#menuone").click isn't attached at the time you trigger the click.
Solution:
$( document ).ready(function() {
var $menuone = $('#menu1');
var $menutwo = $('#menu2');
var $menuthree = $('#menu3');
var $menufour = $('#menu4');
$("#menuone").click(function () {
$("#frm1").show();
$("#frm3").hide();
$("#frm4").hide();
$("#frm2").hide();
$(this).css({border:"2px solid #A69A8F","border-bottom-width":"0", background: "#F7F7F7",color:"#0D638B;"});
$( this ).children().css({color: "#0D638B","font-weight":"bold"} );
});
$menutwo.click(function () {
$menuone.removeAttr( 'style' );$menuone.children().removeAttr( 'style' );
$("#frm1").hide();
$("#frm3").hide();
$("#frm4").hide();
$("#frm2").show();
$(this).css({border:"2px solid #A69A8F","border-bottom-width":"0", background: "#F7F7F7",color:"#0D638B;"});
$( this ).children().css({color: "#0D638B","font-weight":"bold"} );
});
// Put it here
$menuone.trigger("click");
});
By the way, your $menuone
isn't $("#menuone")
but $("#menu1")
, which I guess it is a typo?
Upvotes: 4