Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add ontouch event after onclick on same HTML span tag?

I added a span tag to wrap icon button (icon ion-md-chatbubbles) in my html page, and I added onclick event listener on the same tag

         ...
            <span onclick="(function($){
                $('.nav-tabs a[href=&quot;#tab-manga-discussion&quot;]').tab('show')
            })(jQuery);"
            >
            <a href="#manga-discussion"><i class="icon ion-md-chatbubbles"></i></a>
            </span> 
         ...

In desktop it works fine, however I also need to add a touch event listener in that span tag because in mobile devices an user has to touch twice in the icon button to navigate to nav-tab anchor element (#tab-manga-discussion). I tried to add ontouch event after onclick in the tag but didn’t work

ontouchstart="(function($){
                    $('.nav-tabs a[href=&quot;#tab-manga-discussion&quot;]').tab('show')
                })(jQuery);"

In the end I would have the two events combined on same span tag

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

         ...
            <span onclick="(function($){
                $('.nav-tabs a[href=&quot;#tab-manga-discussion&quot;]').tab('show')
            })(jQuery);"
                ontouchstart="(function($){
                $('.nav-tabs a[href=&quot;#tab-manga-discussion&quot;]').tab('show')
            })(jQuery);"
            >
         ...

How can I solve this?

>Solution :

Intrinsic event attributes, like onclick are awful. They have weird scoping rules and are a form of eval. Best practice is to use the addEventListener method.

The touchstart event does not have an associated intrinsic event attribute. Use addEventListener instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading