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

  • Home
  • SEARCH
  • 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 6030559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:03:15+00:00 2026-05-23T05:03:15+00:00

EDIT: I’ve made the changes Matthew and Yossi suggested and it still doesn’t seem

  • 0

EDIT:

I’ve made the changes Matthew and Yossi suggested and it still doesn’t seem to work. Those changes I’ve edited in the post below too.

It now works!


I have a question for a particular problem I can’t solve. If you know this question has been answered please send me the link as an answer. I’m trying not to use a framework in this case, but can use jQuery if necessary.

I have found answers on how to attach listeners via functions but I need something so as I wouldn’t have to refactor all the code I already have. I’m a freelancer and am working on somebody else’s code.

What happens is that I want to detect a touch event for a touch device. This code should work for a PC too so I need to detect clicks. There’s this DIV which is created programatically to which I need to add the click or touch, depending on the device. Originally the function was called from an onmousedown event like this:

arrDivAnswers[c].onmousedown = onQuestionDown;

And this is the function it calls:

function onQuestionDown(e)
{
    if(!itemSelected)
    {
        if(this.getAttribute('data-isCorrect') == 'true')
            setStyleQCorrect(this, true);
        else
            setStyleQIncorrect(this);

        this.querySelector('.answerText').style.color = '#ffffff';
        this.querySelector('.isCorrect').style.visibility = 'visible';
    }

    itemSelected = true;
}

This was working fine. Now I’ve made this one which would try and select the correct event for a click or touch (I need a function because I have to use this more than once – and the isTouchDevice is working fine. I use that on some other apps so that code is pretty short and has been tested):

function detectEventClickOrTouch(element, functionToCall){
  //detectEventClickOrTouch(arrDivAnswers[c], 'onQuestionDown');

  if(isTouchDevice()){
    element.addEventListener("touchend", functionToCall, false);
  } else{
    element.addEventListener("click", functionToCall, false);
  }
}

The DIV element gets created like this on some loop:

    arrDivAnswers[c] = document.createElement('div');
    console.log( "Answer object #" + c + " = " + arrDivAnswers[c] );
    arrDivAnswers[c].className = 'autosize';
    arrDivAnswers[c].style.textAlign = 'left';
    arrDivAnswers[c].setAttribute('data-isCorrect',false);
    arrDivAnswers[c].setAttribute('data-isSelected',false);
    divAnswerContainer.appendChild(arrDivAnswers[c]);

And then the events get attached to it like this (the older method has been commented out):

for(c;c < arrQuestions[index].arrAnswers.length;c++)
        {
            var curAnswer = arrQuestions[index].arrAnswers[c];
            arrDivAnswers[c].onmouseover = function (e){setStyleQHover(e.currentTarget)};
            arrDivAnswers[c].onmouseout = function (e){setStyleQUp(e.currentTarget)};


            // Detect touch here *************************
            detectEventClickOrTouch(arrDivAnswers[c], onQuestionDown);
            //arrDivAnswers[c].onmousedown = onQuestionDown;
            // Detect touch here *************************

            arrDivAnswers[c].style.visibility = 'visible';
            arrDivAnswers[c].querySelector('.answerText').innerHTML = curAnswer.strAnswer;
            arrDivAnswers[c].setAttribute('data-isCorrect',curAnswer.isCorrect);
            if(curAnswer.isCorrect)
            {
                //arrDivAnswers[c].classList.add("correctAnswer");
                arrDivAnswers[c].className = "correctAnswer";
            }
            else
            {
                //arrDivAnswers[c].classList.remove("correctAnswer");
                arrDivAnswers[c].className = "autosize";
            }
            arrDivAnswers[c].setAttribute('data-isSelected',false);
            setStyleQUp(arrDivAnswers[c]);
            itemSelected = false;
        }
[...]

The debugger is throwing this error:

Uncaught TypeError: Object [object DOMWindow] has no method ‘getAttribute’

I’m sure I’m messing up the "this" because I’m not calling the function properly.

  • 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-23T05:03:16+00:00Added an answer on May 23, 2026 at 5:03 am

    I agree the “this” variable is getting messed up. The problem is that you are attaching an anonymous function as the callback that then calls eval on another method. This seems unnecessary.

    Could you just do this:

    function detectEventClickOrTouch(element, functionToCall){
      //detectEventClickOrTouch(arrDivAnswers[c], 'onQuestionDown');
    
      if(isTouchDevice()){
        element.addEventListener("touchend", functionToCall, false);
      } else{
        element.addEventListener("click", functionToCall, false);
      }
    }
    

    And then when you attach the event just do:

    detectEventClickOrTouch(arrDivAnswers[c], onQuestionDown);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT My jsfiddle entry is here : http://jsfiddle.net/ehNrE/3/ All the codes(only those required) below
EDIT: Ive edited my code a little thanks to Alexander's advice, but im still
EDIT: Updated thanks to @daroczig's lovely answer below. However, test 2 still feels like
Edit: With enough rewriting, and commenting, I have it running, will post final below
EDIT: See my answer below--> I am wanting to have a view that when
EDIT: Here is the edited control file (control.ascx): <%@ Control Language=C# AutoEventWireup=true CodeFile=Sale.ascx.cs Inherits=Enmasse.Modules.Demo_Enmasse.Sale
EDIT: Added debugging output with memory locations as suggested by PlasmaHH. I don't understand
Edit: The below question was answered by this . I have a new updated
Edit: I think I figured out the solution, but I'm still interested to know
Edit: 2020 update -- Please disregard the question below as Google's weather API is

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.