# Pavlovia/JS Mouse Pressed and Mouse Contains

**URL:** <https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921>\
**Category:** Online experiments\
**Created:** [July 7, 2021, 3:20pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921 "2021-07-07T15:20:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [July 7, 2021, 3:20pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/1 "2021-07-07T15:20:51Z")

</div>

I am currently working on a version of the BART task stored here: [Jason Geller / bart\_new\_online · GitLab](https://gitlab.pavlovia.org/Jgeller112/bart_new_online).

I would like to include clicks on two shapes (a plus sign to inflate the balloon) and a stop sign (to stop inflating the balloon).

I currently have been using this code that has been floating around:

```auto
if (plus1.contains(mouse) && mouse.getPressed()[0] === 1){
    nPumps = (nPumps + 1);
    if (nPumps > maxPumps) {
        popped = true;
        continueRoutine = false;
    }
}
if (stop1.contains(mouse) && mouse.getPressed()[0] === 1){
    popped = false;
    continueRoutine = false;
}

```

It allows me to hover over the shapes (plus1 and stop1) to inflate and quit but when I try to click my mouse I get this error:

 ![Screen Shot 2021-07-07 at 11.05.49 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/7/0775194c52d7c6cd2fc590e4ce1ecec83cf15d8c.png)

Any suggestions?

---

<div class="post-metadata">

**Author:** ![thomas\_pronk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/thomas_pronk/32/9531_2.png) [@thomas\_pronk](https://discourse.psychopy.org/u/thomas_pronk)\
**Post date:** [July 8, 2021, 11:50am UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/2 "2021-07-08T11:50:29Z")

</div>

My guess would be that `plus1` and `stop1` aren’t recognized as PsychoJS objects and so don’t provide the `contains` function. Maybe they’ve got subtly different names?

To get a better grasp on this, it could be handy to print these two objects to the console and/or expose them to the window; then you can inspect them while the task is running. See the tutorials over here: [Thomas Pronk / assignment\_stroop · GitLab](https://gitlab.pavlovia.org/tpronk/assignment_stroop#introduction)

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [July 8, 2021, 3:28pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/3 "2021-07-08T15:28:11Z")

</div>

Hi @thomas_pronk,

Thanks for replying! I thought that might be the case as well. I tried calling the shapes as an object.

```auto
var clickable_stimuli = [plus1, stop1]; 

console.log(clickable_stimuli[0])

```

It prints out that it is a shape object.

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [July 8, 2021, 3:42pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/4 "2021-07-08T15:42:46Z")

</div>

Also, I can interact with the objects themselves by hovering over them. It is the clicking action that is not working.

---

<div class="post-metadata">

**Author:** ![thomas\_pronk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/thomas_pronk/32/9531_2.png) [@thomas\_pronk](https://discourse.psychopy.org/u/thomas_pronk)\
**Post date:** [July 9, 2021, 8:41am UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/5 "2021-07-09T08:41:50Z")

</div>

I cloned your repo to take a peek, but first a question: should I look at BART\_v3\_6.22.21\_jf or BART\_v3\_6.22.21\_jg?

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [July 9, 2021, 1:30pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/6 "2021-07-09T13:30:27Z")

</div>

Sorry. BART\_v3\_6.22.21\_jg is the one I am working from.

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [July 12, 2021, 6:44pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/7 "2021-07-12T18:44:31Z")

</div>

@thomas_pronk, any luck?

Seems like there are a few unanswered questions on the forum that would benefit from a solution.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 12, 2021, 7:35pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/8 "2021-07-12T19:35:06Z")

</div>

If you’ve found other people asking the same question, please could you post a list of links to the relevant threads?

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [July 12, 2021, 10:58pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/9 "2021-07-12T22:58:18Z")

</div>

> [@Access mouseclick.clicked\_text works in python but not js](https://discourse.psychopy.org/t/access-mouseclick-clicked-text-works-in-python-but-not-js/12459/2):
>
> In other words: What I need is the option to display text that I just clicked. It works when I start the experiment from the Builder, it does not work when I start it from [Pavlovia.org](http://Pavlovia.org) (as a pilot experiment). JavaScript does not seem to be able to gather that information from the xxxxx.clicked\_text variable.

> [@Clickable Stimuli Not Working Online on Pavlovia](https://discourse.psychopy.org/t/clickable-stimuli-not-working-online-on-pavlovia/23943/3):
>
> Hey , are you talking about the Psychopy Experiment code uploaded on Pavlovia. Its here-

---

<div class="post-metadata">

**Author:** ![thomas\_pronk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/thomas_pronk/32/9531_2.png) [@thomas\_pronk](https://discourse.psychopy.org/u/thomas_pronk)\
**Post date:** [July 13, 2021, 8:46am UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/10 "2021-07-13T08:46:01Z")

</div>

Thnx for the heads-up! Bit busy today but less so tomorrow, so then I’ll take a dive

---

<div class="post-metadata">

**Author:** ![thomas\_pronk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/thomas_pronk/32/9531_2.png) [@thomas\_pronk](https://discourse.psychopy.org/u/thomas_pronk)\
**Post date:** [July 14, 2021, 8:34am UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/11 "2021-07-14T08:34:45Z")

</div>

Thanks for the links to other users reporting click-issues!

Took a little dive, where I explored the issue from two angles.

- I applied your “detect click” logic to one of my demos, and that worked fine. See [Thomas Pronk / demo\_buttons · GitLab](https://gitlab.pavlovia.org/tpronk/demo_buttons)
- I looked around in your generated JS, and there I saw an issue. More below…

In the generated code I saw this…

```auto
          for (const obj of [[plus1, stop1]]) {
            if (obj.contains(mouse_real)) {
              gotValidClick = true;
              mouse_real.clicked_name.push(obj.name)
            }
          }

```

Note the double set of brackets. That means this code defines an array with one element, whose value is an array `[plus1, stop1]`. Next, we treat this element as VisualStim (and check whether this element contains the mouse by calling `.contains` on it.

Tracing this to the builder, the issue is likely the “Clickable stimuli” field in the “mouse\_real” Mouse component. Instead of having `[plus1, stop1]` there, try `plus1, stop1`

---

<div class="post-metadata">

**Author:** ![jgeller112](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jgeller112/32/9835_2.png) [@jgeller112](https://discourse.psychopy.org/u/jgeller112)\
**Post date:** [July 14, 2021, 9:45pm UTC](https://discourse.psychopy.org/t/pavlovia-js-mouse-pressed-and-mouse-contains/23921/12 "2021-07-14T21:45:42Z")

</div>

Thanks for trying to figure this out @thomas_pronk

Upon further investigation, the mouse was saving on every click, and not every frame.
