Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #36845

Re: Getting the index of a clicked element

From James Harris <james.harris.1@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Getting the index of a clicked element
Date 2019-06-08 18:57 +0100
Organization A noiseless patient Spider
Message-ID <qdgstj$ltv$1@dont-email.me> (permalink)
References <qde3ou$aqm$1@dont-email.me> <e1c1be71-2179-492d-934e-bb47d477ebce@googlegroups.com>

Show all headers | View raw


On 07/06/2019 18:45, Julio Di Egidio wrote:
> On Friday, 7 June 2019 18:35:49 UTC+2, James Harris  wrote:
> 
>> My problem is in identifying the corresponding answer when a
>> question is clicked.
> 
> The canonical way is the questions originally are numbered,
> then you can use element id's to refer to specific elements,
> plus you do not have the problem that any change in the structure
> of your html breaks the whole thing (IOW, you do not tie the
> functionality to the specific markup).
> 
>> <div class = "question" id="q_123">Why?</div>
>> <div class = "answer" id="a_123">Because</div>

I didn't want to put the user through the hassle of having to assign 
unique ids but I used your idea to generate the ids automatically with

   var questions = document.getElementsByClassName(QCLASS);

   // Go through each question
   for (question of questions)
   {
     // Find the following answer
     targ = next_sibling(question, Node.ELEMENT_NODE);
     if (targ && targ.className === ACLASS)
     {
       // Give the question a unique id
       question.id = QID + i;

Then the id is used as a key to save the answer text in an array with

       // Move the answer out of the div into the designated array
       saved_answers[question.id] = targ.innerHTML;
       targ.innerHTML = BLANK_ANSWER;

       // Next id number
       i++;
     }
   }



-- 
James Harris

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Getting the index of a clicked element James Harris <james.harris.1@gmail.com> - 2019-06-07 17:35 +0100
  Re: Getting the index of a clicked element James Harris <james.harris.1@gmail.com> - 2019-06-07 17:59 +0100
    Re: Getting the index of a clicked element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-07 20:33 +0200
      Re: Getting the index of a clicked element dr.j.r.stockton@gmail.com - 2019-06-08 01:43 -0700
  Re: Getting the index of a clicked element "R.Wieser" <address@not.available> - 2019-06-07 19:39 +0200
    Re: Getting the index of a clicked element James Harris <james.harris.1@gmail.com> - 2019-06-08 18:52 +0100
  Re: Getting the index of a clicked element Julio Di Egidio <julio@diegidio.name> - 2019-06-07 10:45 -0700
    Re: Getting the index of a clicked element James Harris <james.harris.1@gmail.com> - 2019-06-08 18:57 +0100
      Re: Getting the index of a clicked element Julio Di Egidio <julio@diegidio.name> - 2019-06-09 05:44 -0700
  Re: Getting the index of a clicked element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-07 20:16 +0200
    Re: Getting the index of a clicked element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-07 22:48 +0200
  Re: Getting the index of a clicked element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-07 20:36 +0200
  Re: Getting the index of a clicked element JJ <jj4public@vfemail.net> - 2019-06-08 12:57 +0700
    Re: Getting the index of a clicked element James Harris <james.harris.1@gmail.com> - 2019-06-08 10:30 +0100
      Re: Getting the index of a clicked element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-08 12:13 +0200
        Re: Getting the index of a clicked element John G Harris <niam@jghnorth.org.uk.invalid> - 2019-06-08 13:35 +0100
        Re: Getting the index of a clicked element John G Harris <niam@jghnorth.org.uk.invalid> - 2019-06-08 13:45 +0100
        Re: Getting the index of a clicked element James Harris <james.harris.1@gmail.com> - 2019-06-08 19:03 +0100
          Re: Getting the index of a clicked element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-08 21:11 +0200
            Re: Getting the index of a clicked element dr.j.r.stockton@gmail.com - 2019-06-08 14:18 -0700
  Re: Getting the index of a clicked element dr.j.r.stockton@gmail.com - 2019-06-08 02:16 -0700
    Re: Getting the index of a clicked element James Harris <james.harris.1@gmail.com> - 2019-06-08 19:14 +0100
  Re: Getting the index of a clicked element dr.j.r.stockton@gmail.com - 2019-06-10 11:29 -0700
    Re: Getting the index of a clicked element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-06-10 23:00 +0200
      Re: Getting the index of a clicked element dr.j.r.stockton@gmail.com - 2019-06-11 03:22 -0700

csiph-web