I have the following in my Go's template.
<button type="submit" class="validate_button" onclick="validate({{.Quiz.ID}})">Validate</button>
This is not working and I am getting this Javascript error :
"Property assignment expected."
This works perfectly of course :
<button type="submit" class="validate_button" onclick="validate(1234)">Validate</button>
Whats the right syntax here?
Note :I am using VS Code. The same is telling me there is an error but I can compile just fine. The thing is once I ran the app there is no "button" on the page. It simply disappears. It works correctly when I hardcode a value though.
Note 2 :
This is the ouput of executing my template :
2020/02/13 09:52:49 template: execute.html:38:79: executing "execute.html" at <.Quiz.ID>: can't evaluate field Quiz in type models.Question
This is my template :
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Linux Quiz</title>
<meta name="description" content="Linux Quiz">
<meta name="author" content="Matias Barrios">
<link rel="stylesheet" href="static/executeQuizz/style.css">
<link rel="stylesheet" href="static/style.css">
<link rel="stylesheet" href="static/bootstrap/css/bootstrap.css">
<script src="static/bootstrap/js/bootstrap.min.js"></script>
<script src="static/executeQuizz/main.js"></script>
</head>
<body>
<div class="topnav">
<a class="active" href="/index">Home</a>
<a href="/logout" class="righty" ><div>Logout : <b>{{ .User.Name }}</b></div></a>
<a href="/contact" class="righty" >Contact</a>
<a href="/about" class="righty" >About</a>
</div>
<div class="title_and_description" >
<h5> {{ .Quiz.ID }} </h5>
<h6> {{ .Quiz.Description }} </h6>
</div>
{{range $q := .Quiz.Questions}}
<div class="question_container">
<p><h4>{{ $q.Question }}</h4></p>
{{ $mode := $q.Mode }} {{ if eq $mode "single" }}
{{range $o := $q.Options}}
<input type="radio" id="{{ $q.ID }}" name="{{ $q.ID }}" value="{{$o}}">
<label for="{{ $q.ID }}">{{$o}}</label><br>
{{end}}
{{ end}}
</div>
<div class="validate_quiz">
<button type="submit" class="validate_button" onclick="validate({{.Quiz.ID}})">Validate</button>
</div>
{{ end }}
</body>
</html>
And this is the execution of the template :
func executeQuizzHanlder(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/execute" && r.Method != "GET" {
errorHandler(w, r, http.StatusNotFound)
return
}
claims := getClaims(w, r)
u := models.User{
Name: claims.Username,
}
keys, ok := r.URL.Query()["quizz"]
if !ok || len(keys) < 1 || len(keys[0]) < 1 {
errorHandler(w, r, http.StatusNotFound)
return
}
quizz, err := quizzes.GetQuizzByID(keys[0])
if err != nil {
errorHandler(w, r, http.StatusNotFound)
return
}
quizz.ID = keys[0]
envelope := models.ExecuteQuizzEnvelope{
User: u,
Quiz: quizz,
}
err = views.ViewExecuteQuizz.Execute(w, &envelope)
log.Println(err.Error())
}
And this is my models :
package models
// Question :
type Question struct {
ID string `json:"id"`
Mode string `json:"mode"`
Question string `json:"question"`
Options []string `json:"options"`
Answers []string `json:"answers"`
}
// Quiz :
type Quiz struct {
ID string `json:"id"`
Description string `json:"description"`
Author string `json:"author"`
Questions []Question `json:"questions"`
Completed bool `json:"-"`
}
// ExecuteQuizzEnvelope :
type ExecuteQuizzEnvelope struct {
User User
Quiz Quiz
}