Having error in JQuery events | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

23rd May 2018, 7:37 AM
Akshat Vira
Akshat Vira - avatar
10 Answers
+ 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') });
23rd May 2018, 8:42 AM
KrOW
KrOW - avatar
+ 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
23rd May 2018, 8:21 AM
KrOW
KrOW - avatar
+ 1
KrOW Thanks a lot 😊
23rd May 2018, 8:43 AM
Akshat Vira
Akshat Vira - avatar
+ 1
👍👍👍👍
23rd May 2018, 8:44 AM
KrOW
KrOW - avatar
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') });
23rd May 2018, 7:56 AM
KrOW
KrOW - avatar
0
KrOW I tried it but it's not working
23rd May 2018, 8:06 AM
Akshat Vira
Akshat Vira - avatar
0
Akshat Vira Works to me... What are you error??
23rd May 2018, 8:08 AM
KrOW
KrOW - avatar
0
KrOW I'm not getting error but the tab isn't opening...maybe the function is not working
23rd May 2018, 8:09 AM
Akshat Vira
Akshat Vira - avatar
0
KrOW Thanks ... Also can you explain me , I didn't understand it or simply can u add comments ...
23rd May 2018, 8:33 AM
Akshat Vira
Akshat Vira - avatar
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 ??
23rd May 2018, 8:37 AM
Akshat Vira
Akshat Vira - avatar