Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6912065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:02:35+00:00 2026-05-27T09:02:35+00:00

I am programming a website mainly targetted at iOS Safari, but I also want

  • 0

I am programming a website mainly targetted at iOS Safari, but I also want it to respond to normal desktops.

So I have a need to make a button react to the events onmousedown onmouseup when on a desktop & for a button to react to ontouchstart ontouchend on iOS.

NOTE: I cannot do this(just register for both events):

<img id="button" onmousedown="this.src=''" onmouseup="this.src=''; doSomething();" ontouchstart="this.src=''" ontouchend="this.src=''"

Whilst this will work on desktop, on iOS, the image src does not change, something to do with the order of events iOS mimics.

So I am trying to transfer the javascript in ontouchstart into onmousedown but I get the error “eles[i] is undefined” when I press the button, the elements EXIST & have the correct ID’s:

function enableDesktopGestures()
{
  var eles = new Array(document.getElementById("topicIndex"),
                       document.getElementById("volumeToggle"),
                       document.getElementById("exitModuleBt"),
                       document.getElementById("prevBt"),
                       document.getElementById("nextBt"));

  for (var i=0; i<eles.length; i++)
  {
     eles[i].addEventListener("mousedown", function() { eles[i].getAttribute("ontouchstart"); }, false);
     eles[i].addEventListener("mouseup", function() { eles[i].getAttribute("ontouchend"); }, false);
  }
}

// No error occurs with this but when I press the buttons nothing happens
function enableDesktopGestures()
{
  var eles = new Array(document.getElementById("topicIndex"),
                       document.getElementById("volumeToggle"),
                       document.getElementById("exitModuleBt"),
                       document.getElementById("prevBt"),
                       document.getElementById("nextBt"));

  for (var i=0; i<eles.length; i++)
  {
     var down = eles[i].getAttribute("ontouchstart");
     var up   = eles[i].getAttribute("ontouchend");  
     eles[i].onmousedown = function() { down; };
     eles[i].onmouseup   = function() { up; };
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:02:36+00:00Added an answer on May 27, 2026 at 9:02 am

    The problem in your second example is that your added listener results in the following code (or something alike):

    function() { "valueOfontouchstartAttribute"; }
    

    Setting the onmousedown attribute like below seems to work for me, but then you might still run into your order of events problem.

    eles[i].onmousedown = down;
    

    The first example is a little quirky. If you read out the value of your i-iterator in the for-loop from within the scope of the listener you defined it will have the value of the length of the eles array, this is probably because your listener code is evaluated in a different scope. Scope fix:

    for(var i = 0; i < eles.length; i++) {
      var curEl = eles[i];
      eles[i].addEventListener("mousedown", function() { curEl.getAttribute("ontouchstart"); }, false);
    }
    

    Also in your first example your code should fail because (like in the first example) the value of the ontouchstart attribute does not get evaluated. Wrapping the attribute value with eval seems to be an option, but I’d say that is pretty dangerous, unless you’re sure you have full control over these attribute values ( When is JavaScript's eval() not evil? )… Just in case:

    for(var i = 0; i < eles.length; i++) {
      var curEl = eles[i];
      eles[i].addEventListener("mousedown", function() { eval(curEl.getAttribute("ontouchstart")) }, false)
    }
    

    One more question: which browsers do you wish to support when referring to normal desktops? A lot of users still use IE < 9, I read the addEventListener method is then not supported:
    addEventListener is not working

    In this case you could use first test for the existence of the addEventListener method and else fall back on attachEvent.

    Wouldn’t it be easier in general to attach all the listeners on page load in stead of copying them from the HTML?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a headline on a website I'm programming; let's say the headline says
Ok, I am programming a website and I need to do a lot of
I am programming my first real PHP website and am wondering how to make
I want to learn how to make a website with C#. I know PHP,
I am programming a website for my school and I wanted to make the
I'm just started with GIS programming. I want to build a simple website with
I'm programming a website and on a page I have dynamic content. To be
Recently I'm programming a website and I want to prevent my source code from
I have very little background in website programming so I apologise if this might
You and I want to be the expert on computer programming or website design,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.