Solved

jQuery touch to .Open and .Close

Posted on 2013-02-03
8
582 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
ID: 38848664
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
ID: 38850289
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
ID: 38850527
Thanks for the contributions guys!

But I've not been able to use touch to close using either of the above methods^
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38852210
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
 
LVL 6

Expert Comment

by:lightspeedvt
ID: 38882277
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
ID: 38886946
^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
ID: 38886962
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
ID: 38946712
Sorry for not closing this sooner
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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 receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

837 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