How use a function with Template

Hello,

I have a Template in my project.

It’s :

However, in my .js i can’t use my class=“test” in my template

In my .js :

Template.Accueil.events({
“submit .test”: function() {
event.target.name_equipement.value=“Vad”;
}
});

And don’t work. So, how work with my class=“test” because if i change in my .js with my other class=“new-task” this code work. Why this code don’t work with the class of my button but he works with the class of my form ?

Best Regards,

Please insert 3 backticks on a line before your template and 3 backticks on a line after your template (we can’t see it!):

```
<template ...
...
```

Also the same for your js, please :slight_smile:

Ok i’m sorry

Hello,

I have a Template in my project.

It’s :

<template name="Accueil>
<input type="text" id="id_equipement" class="validate">
<input type="text" id="name_equipement" class="validate">
<input type="submit" id="send_equipement class="test">
</template

However, in my .js i can’t use my class=“test” in my template

In my .js :

Template.Accueil.events({
"submit .test": function() {
event.target.name_equipement.value="Vad";
}
});

And don’t work. So, how work with my class=“test” because if i change
in my .js with my other class=“new-task” this code work. Why this code
don’t work with the class of my button but he works with the class of my
form ?

Best Regards,

Your html is missing a close quote on the id of your submit input.

yes thank you but it’s not the error because i just add the quote in my project and i have always the problem.

The submit event is fired when a form is submitted.
Note that submit is fired only on the form element, not the button or submit input. (Forms are submitted, not buttons.)

Use click instead.

Ok but in my event click after the “.” I choose the class of my button ? So, I choose :

Template.Accueil.events({
"click .test": function(event) {
event.target.name_equipement.value="Vlad";
return false;
}

}); 

Because this code doesn’t work in my project. I have 0 error but my value of my name_equipement don’t change. It’s normal ?

  1. Why a submit button when you’re not going to “submit” anything?
  2. What else is the button going to do besides setting the equipment name to “Vlad”?

With my code in .js, I want that my button click change the value of the text field “name_equipement” in " Vlad " . This is only an example to design something else after .

However now, with my actually code I don’t arrive to change the value of the text fiel “name_equipement”.

Can you help me with the Template.event ?

With plain Blaze you have to use jQuery:

<template name="Accueil">
  <input type="text" id="name_equipement" class="validate">
  <input type="submit" id="send_equipement" class="test">
</template>
Template.Accueil.events({
  "click .test": function(e, template) {
    template.$("#name_equipement").val("Vad");
  }
});

It tends to get messy and error prone, take a look at the ViewModel way of doing things:

<template name="Accueil">
  <input type="text" class="validate" {{b "value: name"}}>
  <input type="submit" {{b "click: updateName"}}>
</template>
Template.Accueil.viewmodel({
  updateName() {
    this.name("Vad");
  }
});
1 Like

Thanks it’s working. I’m very happy.I would not Succeed without you