+ 1
Having error in JQuery events
I'm trying to make a tab as Default in the following code but somehow it's not working. Can anyone help me to solve it? My thought process is to call a function 'openTab' as soon as the DOM loads... Things which I tried: 1. document.getElementById("hm").click(); 2. $('#hm').on('click', openTab(event,'home'); https://code.sololearn.com/WDoYocorzI3B/?ref=app
10 odpowiedzi
+ 1
$(document).ready(function(){
// get a reference to default tab
var defTab= document.getElementById('hm')
// your openTab function
// need to event.currentTarget
// and we are faked it
var fakeEv= {
currentTarget: defTab
}
// call openTab with faked
// event
openTab(fakeEv, 'home')
});
+ 1
Akshat Vira Ok i see better your problem... Remove .on call and replace it with:
$(document).ready(function(){
var defTab= $('#hm')
var fakeEv= {
currentTarget: defTab
}
openTab(fakeEv, 'home')
});
This simply open the default tab (at document loaded) and use a fake event for get worjing your openTab function
+ 1
KrOW Thanks a lot 😊
+ 1
👍👍👍👍
0
you assign click listener in wrong way because you call openTab function and not pass a reference to .on function... This cause a classic "access to DOM before that its loaded" error also... You have to pass a function reference to .on function that wrap a call to openTab function...
$('#hm').click(function(){
openTab(event,'home')
});
0
KrOW I tried it but it's not working
0
Akshat Vira Works to me... What are you error??
0
KrOW I'm not getting error but the tab isn't opening...maybe the function is not working
0
KrOW Thanks ...
Also can you explain me , I didn't understand it or simply can u add comments ...
0
KrOW I tried this...
$(document).ready(function (){
openTab(event,'home')
});
This gives an error for current Target (as #hm didn't made the call)
Am I correct ??