# Cannot even get a button working. What am I missing?

**URL:** <https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [May 23, 2020, 8:44am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236 "2020-05-23T08:44:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ict](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@ict](https://forum.wixstudio.com/u/ict)\
**Post date:** [May 23, 2020, 8:44am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/1 "2020-05-23T08:44:44Z")

</div>

Hi,

I have a dynamic page with a button.  
With this button I want to open a lightbox.  
I tried and tried but not working.  
Now I have hardcoded the name of the lightbox in it, and even then it’s not opening.  
Am I missing something?

I have set the name in the properties of the lightbox to HRMCoaching.  
I put the code like hereunder. And then in preview click the button.\>\> Nothing happens  
Any suggestions?

import wixData **from** ‘wix-data’ ;  
import {wixWindow} **from** “wix-window” ;  
import {local} **from** ‘wix-storage’ ;

$w.onReady( **function** () {  
$w( ‘#button8’ ).onClick((event)=\>{  
wixWindow.openLightbox( ‘#HRMCoaching’ )  
});  
/\*  
$w.onReady(function () {  
let dienst = local.getItem(‘#projectname2’);  
console.log(dienst)  
wixData.query(“Projects”)  
.eq(“title”, dienst)  
.find()  
.then(results =\> {  
let items = results.items;  
let firstItem = items[0];  
_/  
/_  
$w(`#button8`).onClick((event)=\>{  
wixWindow.openLightbox(“HRM Coaching”);  
[//wixWindow.openLightbox](https://wixWindow.openLightbox)(firstItem.lightbox);  
//});  
\*/  
//});  
});

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [May 23, 2020, 9:48am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/2 "2020-05-23T09:48:06Z")

</div>

See the Wix Window API for more help with how to open a lightbox.  
[https://www.wix.com/corvid/reference/wix-window.html#openLightbox](https://www.wix.com/corvid/reference/wix-window.html#openLightbox)

**Open a lightbox**

```javascript
import wixWindow from 'wix-window';

// ...

wixWindow.openLightbox("LightboxName");

```

---

<div class="post-metadata">

**Author:** ![ict](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@ict](https://forum.wixstudio.com/u/ict)\
**Post date:** [May 23, 2020, 10:35am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/3 "2020-05-23T10:35:35Z")

</div>

Ok thanks, got this working.

Now I’m trying to open a Lightbox Dynamically  
Depending on the record in the database. In d=the database there’s a field lightbox, where I have filled in the name of the Lightbox. But this doesn’t work yet ☹  
Any idea pls?

**import** wixData **from** ‘wix-data’ ;  
**import** wixWindow **from** “wix-window” ;  
**import** {local} **from** ‘wix-storage’ ;

/\*  
$w.onReady(function () {  
$w(‘#button8’).onClick((event)=\>{  
wixWindow.openLightbox(‘HRM Coaching’)  
});

\*/

$w.onReady( **function** () {  
**let** dienst = local.getItem( ‘#projectname2’ );  
console.log(dienst)  
wixData.query( “Projects” )  
.eq( “title” , dienst)  
.find()  
.then(results =\> {  
**let** items = results.items;  
**let** firstItem = items[0];

$w(`#button8`).onClick((event)=\>{

```
            wixWindow.openLightbox(firstItem.lightbox); 
    }); 

}); 

```

});

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [May 23, 2020, 11:11am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/4 "2020-05-23T11:11:09Z")

</div>

Simply read the Wix API Reference for help with this.  
[https://www.wix.com/corvid/reference/wix-window.lightbox.html](https://www.wix.com/corvid/reference/wix-window.lightbox.html)

Or make use of the search function in this forum as there are many previous posts about this.  
[https://www.wix.com/corvid/forum/community-discussion/dynamic-page-lightboxes](https://www.wix.com/corvid/forum/community-discussion/dynamic-page-lightboxes)  
[https://www.wix.com/corvid/forum/community-discussion/lightbox-with-dynamic-content-via-button](https://www.wix.com/corvid/forum/community-discussion/lightbox-with-dynamic-content-via-button)

**How do I pass data between a page and a lightbox?**

When you open a lightbox using the [openLightbox()](https://www.wix.com/code/reference/wix-window.html#openLightbox) function, you can pass an object containing data to be used in the lightbox. In the lightbox’s code, you call the [getContext()](https://www.wix.com/code/reference/wix-window.lightbox.html#getContext) function to retrieve the data sent by the [openLightbox()](https://www.wix.com/code/reference/wix-window.html#openLightbox) function.

When you close the lightbox using the [close()](https://www.wix.com/code/reference/wix-window.lightbox.html#close) function, you can pass an object containing data to be used by the page that opened the lightbox. This data is retrieved from the resolution of the Promise returned by the [openLightbox()](https://www.wix.com/code/reference/wix-window.html#openLightbox) function.

**A scenario where information is passed between a page and a lightbox**  
This example demonstrates how to pass data from a page to a lightbox that it opens and from the lightbox back to the page as it closes.

It assumes that the page has:

- An open button that is used to open the lightbox.

- Two text inputs where information that is to be passed to the lightbox is entered.

- Two text elements where information that is passed from the lightbox is displayed.

It assumes that the lightbox has:

- An close button that is used to close the lightbox.

- Two text inputs where information that is to be passed to the page is entered.

- Two text elements where information that is passed from the page is displayed.

```javascript
/ *************
 * Page Code *
 ************* /

import wixWindow from 'wix-window';

export function openButton_click(event) {
  wixWindow.openLightbox("MyLightBox", {
    "pageSend1": $w('#pageSend1').value,
    "pageSend2": $w('#pageSend2').value
  })
  .then( (data) => {
    $w('#pageReceive1').text = data.lightboxSend1;
    $w('#pageReceive2').text = data.lightboxSend2;
  } );
}

/ *****************
 * Lightbox Code *
 ***************** /

import wixWindow from 'wix-window';

$w.onReady( function () {
  let received = wixWindow.lightbox.getContext();
  $w('#lightBoxReceive1').text = received.pageSend1;
  $w('#lightBoxReceive2').text = received.pageSend2;
} );

export function closeButton_click(event) {
  wixWindow.lightbox.close( {
    "lightBoxSend1": $w('#lightBoxSend1').value,
    "lightBoxSend2": $w('#lightBoxSend2').value
  } );
}

```

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [May 23, 2020, 11:13am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/5 "2020-05-23T11:13:36Z")

</div>

This is wrong here.

```javascript
$w(`#button8`).onClick((event)=>{ 

```

Don’t use the ` for elements as it needs to be like you have already used above that is now marked out.

```javascript
$w('#button8').onClick((event)=>{ 

```

You use the ` when you are moving to another page through button click.

```javascript
export function myaccountbutton_onclick(event) {
 wixLocation.to(`/account/my-account`); 

```

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [May 23, 2020, 11:21am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/6 "2020-05-23T11:21:53Z")

</div>

Also change your code to something like this.

```javascript
find() \
  .then( (results) => { \
    lightbox = results.items[0].lightbox;
  } );

```

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [May 23, 2020, 11:30am UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/7 "2020-05-23T11:30:40Z")

</div>

Also when you use Wix Storage you need to be storing the variable correctly.  
[https://www.wix.com/corvid/reference/wix-storage.Storage.html#setItem](https://www.wix.com/corvid/reference/wix-storage.Storage.html#setItem)  
[https://www.wix.com/corvid/reference/wix-storage.Storage.html#getItem](https://www.wix.com/corvid/reference/wix-storage.Storage.html#getItem)

**Examples**  
Store an item in local storage

```javascript
import {local} from 'wix-storage'; // ... local.setItem("key", "value");

```

Key can be whatever name you want the stored item to be called.  
Value is where you add the element name, so in your case it would be $w(‘#projectname2’).value.

Examples  
Retrieve an item from local storage

```javascript
import {local} from 'wix-storage';

// ...

let value = local.getItem("key"); // "value"

```

The value returned here will be the value that is assigned to the name that you have added to the getItem line, so here it would be the value that is associated with ‘key’

Just note that if the ‘#projectname2’ element is a text box, then you might need to change .value to .text instead.

---

<div class="post-metadata">

**Author:** ![ict](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@ict](https://forum.wixstudio.com/u/ict)\
**Post date:** [May 24, 2020, 7:28pm UTC](https://forum.wixstudio.com/t/cannot-even-get-a-button-working-what-am-i-missing/21236/8 "2020-05-24T19:28:09Z")

</div>

Thanks for your extensive explanations!  
It got me some further, but since it is not my basic language I have to dig deeper in it to better understand it.

I had a deadline so I took the help of an external.  
Anyway, this is the final result:

**import** wixData **from** ‘wix-data’ ;  
**import** wixWindow **from** “wix-window” ;  
**import** { local } **from** ‘wix-storage’ ;

$w.onReady( **function** () {  
$w( ‘#button8’ ).onClick((event) =\> {  
**let** currentItem = $w( ‘#dynamicDataset’ ).getCurrentItem()  
**let** lightBox = currentItem.lightbox  
wixWindow.openLightbox(lightBox)  
});  
});

Thanks again!
