event.preventDefault();
// Get value from form element
var name = event.target.nameField.value;
// Insert a user into the collection
Tasks.insert({
username: name,
});
// Clear form
event.target.nameField.value = "";
$("#UserNameModal").modal("hide");
},
I finally I figured it out to make it less messy and I modified my code to make it with submit .form, but 2 more issues console.log are not showing in the console browser and my insert neither… Any idea ?
Here is my new code
<div class="modal" id="UserNameModal">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<form> <!-- without submit fonction is not possible -->
<div class="input-group">
<input type="text" class="form-control" autofocus="" placeholder="Insert your name" id="nameField" >
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" id="okInsertUser">Add me now</button>
</span>
</div>
</form>
</div>
</div>
</div>
my event function
“submit .form”: function (event) {
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
var name = event.target.nameField.value;
console.log(nameField);
// Insert a user into the collection
Users.create({
username: name,
});
// Clear form
event.target.nameField.value = "";
$("#UserNameModal").modal("hide");
}
@mrzafod Thanks for your answer, I just tried it, but if i do submit form nothing works. with submit .form at leak my modal close when I type “enter” or when I click the button.
i think you should get and completed the basic tutorial… if you have reached the level 4 or 5 the tutorial, you should probably through this…
here i help you fix this first… from what i learn :
if you want to insert something in <form> you have to define the name of the class…
you have to define the ‘name’ property of your textbox… if you want to use the ‘id’ property, you should use $
you have to define the collection first
the syntax to insert to the collection is not like that…
here’s the code
hmtl
<div class="modal" id="UserNameModal">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<form class="InsertTest"> <!-- without submit fonction is not possible -->
<div class="input-group">
<input type="text" class="form-control" autofocus="" placeholder="Insert your name" id="nameField" name="textbox1">
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" id="okInsertUser">Add me now</button>
</span>
</div>
</form>
</div>
</div>
</div>
.js
'submit .InsertTest': function (event) {
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
var name = event.target.textbox1.value;
alert(name);
// Insert a user into the collection
Users.insert({
username: name,
});
// Clear form
event.target.textbox1.value = "";
$("#UserNameModal").modal("hide");
}
});
dont forget to create the collection first in the top row of your .js
sorry for my bad english… hope you can understand it