zserge / lorca

Build cross-platform modern desktop apps in Go + HTML5
MIT License
8.04k stars 535 forks source link

Using "this" keyword inside bind function #94

Closed Tekameikite closed 4 years ago

Tekameikite commented 4 years ago

I have a bind function like this:


ui.Bind("changeTheme", func() {
      colors := ui.Eval(`document.getElementsByTagName("span")[0].getAttribute("style");`)
      fmt.Println("Colors:", colors)`})

What I need is an element like this:


<div onclick="changeTheme(this)">
<span style="color:#000000;background-color:#FFFF00">foo</span>
</div>

and coresponding function:


function changeColor(element) {
          var theme= element.getElementsByTagName("span")[0].getAttribute("style");
          console.log(theme)
      }

I have to get "theme" element and assign it to a variable in Go

I tried doing it like this with the same html element but it's not working


ui.Bind("changeTheme", func(element lorca.Value) {
      colors := ui.Eval(`element.getElementsByTagName("span")[0].getAttribute("style");`)
      fmt.Println("Colors:", colors)
  })
Tekameikite commented 4 years ago

<div onclick="changeTheme(518)">
<span style="color:#000000;background-color:#FFFF00">foo</span>
</div>

```go
ui.Bind("changeTheme", func(index int) {
      colors := ui.Eval(fmt.Sprintf(`document.getElementsByTagName("span")>[%v].getAttribute("style");`, index)).String()
      fmt.Println("Colors:", colors)
  })

I basically got it to work like that with the index of span element but I have like 518 elements that use this function so it's not that efficient compared to using "this" keyword