Lawl Lawl -4 years ago 98
Javascript Question

How do I keep track of all inputs that have been submitted

I'm trying to keep track every time someone submits a name using

data-name
only. How do I alert the user to let them know that the contents were posted to the website or not and what other names have been submitted.

<div id="container">
<form method="POST" action="#">
<label class="large">Please enter your name
<input id="textbox" class="name" data-name="" pattern="[a-zA-Z0-9]{1-45}">
</label>
</form>
</div>


This is the JQuery I'm using:

//When the form is submitted
$("#form").submit(function(){
//get the value of the input
var input = $('#textbox').val();

//insert it into the data-name attribute
var name = $("#textbox").attr("data-name", input);

//Aler the user
$("input[data-name]").val(function(){
alert($(this).data('name'));
});

});

Answer Source

I think that your jquery selector is incorrect.

$("#form") means that it is targeting an element with the id of "form". In order to make that work, you would need to give your form element an id of form. E.g. <form id="form" method="POST" action="#">

In order to alert the value of the textbox however, you do not need to put the value into a data-name attribute but instead get the value out of the form when submitting.

$('#myForm').submit(function(e){
    alert(e.target.foo.value);
})

See the following fiddle, for an example:

https://jsfiddle.net/dgw3bw63/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download