Help with passing a variable from Lua to Javascript using sendNUI

I want to pass the variable bt from Lua to JS to create a button but it’s not working. I get error in the console “cannot read property “hwButton” of undefined.”

Client:

RegisterNUICallback('NUITest', function(data)
  SetNuiFocus(true, true)
  local bt = "Hello World"
  
  SendNUIMessage({
  type = "myVar",
  hwButton = bt
})
  
end)

HTML/JS:

<script>
	$(function() {
		window.addEventListener('message', function(event) {
		 if (event.data.type == "myVar"){
				var btn = document.createElement("BUTTON");
				var t = document.createTextNode(hwButton);
				btn.appendChild(t);
				document.body.appendChild(btn);
			}
		});
	});
	</script>

 <button class="test"></button>

$(".test").click(function(){
		$.post('http://test/ SOME FUNCTION', JSON.stringify({}));
});

You need to access it using event.data.hwButton

So… var t = document.createTextNode(event.data.hwButton);

1 Like

Beautiful, thank you!

1 Like