Solved

jQuery touch to .Open and .Close

Posted on 2013-02-03
8
576 Views
Last Modified: 2013-03-02
Hi people,

Does anyone know how to replicate the hover function on touch devices?

I am able to open a dropdown menu with touch but I can seem to get it to close when touched!

<script>
<-- Below opens the dropdown -->
jQuery('.dropdown-toggle').on('touchstart', function(e) {		 
	e.stopPropagation();
   var dropdown = jQuery(this).open(".dropdown-menu");

<-- Now I am trying to close on touch --> 
   jQuery('.dropdown-toggle').on('touchstart', function(e) {		 
	e.stopPropagation();
   var dropdown = jQuery(this).removeclass(".open");
   });
});
</script>

Open in new window


Any able to point me in the right direction will be much appreciated...
0
Comment
Question by:rafique12
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
I don't work with mobile, but I have seen things like this:

$("a").live("touchstart", function(e) {
  $(this).trigger("hover");
});


Cd&
0
 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 250 total points
Comment Utility
In order to make the on function work put an additional dom element in it like in the code block. In order to know that your touchstart is triggered insert some console.log()
 statements, so you definitely know it is triggered at all.

Yet another thing, in the jquery mobile docs I couldn't find such method as open(), what I found was popup( open ) or dialog( open ). You consider using toggleSlide() instead of open.
<script>
<-- Below opens the dropdown -->
jQuery('.dropdown-toggle').on('touchstart', 'li', function(e) {            
      e.stopPropagation();
   var dropdown = jQuery(this).toggleSlide(".dropdown-menu", 400);

<-- Now I am trying to close on touch -->
   jQuery('.dropdown-toggle').on('touchstart', 'li', function(e) {            
        console.log('submenu hiding triggered');
      e.stopPropagation();
        var dropdown = jQuery(this).toggleSlide(".dropdown-menu", 400);
   });
});
</script>
0
 

Author Comment

by:rafique12
Comment Utility
Thanks for the contributions guys!

But I've not been able to use touch to close using either of the above methods^
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
Then maybe you should post a link instead of expecting a solution when all you have given us is a fragment of code with no context.

Cd&
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:lightspeedvt
Comment Utility
Same as JQuery library are you sure that you have jQuery mobile library loaded to the site too (http://jquerymobile.com/)?
0
 

Author Comment

by:rafique12
Comment Utility
^The link is dead, but yes I have it loaded.

Thanks for the response though!
0
 
LVL 6

Accepted Solution

by:
lightspeedvt earned 250 total points
Comment Utility
Link is:

http://jquerymobile.com/

If you have it loaded too, in this case it is really needed to see the link, as there could be a lot of things that may break functions working correctly.
0
 

Author Closing Comment

by:rafique12
Comment Utility
Sorry for not closing this sooner
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Javascript in a user control not working 17 52
Responsive CSS question 2 37
HTML button disabled 11 35
onOpen 14 38
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now