Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind an variable to be the value of my html input in jquery

I have a form with a method post, but I have to encrypt some of the parameters to md5 before I post them in my javascript. How do I bind the values to that form.

html form:

<input id="sig"  name="signature" >signature <i class="fa fa-usd" aria-hidden="true">
</i></br>

javascript:

vm.sig_md5 = $.md5('value');
like image 229
Pickles Avatar asked Sep 14 '25 22:09

Pickles


2 Answers

Use jQuery .submit() to modify the value at the time of form submission (value is modified before submit, then submit continues).

HTML:

<form action="somewhere.php" method="post">
    <input id="sig"  name="signature" >signature <i class="fa fa-usd" aria-hidden="true"></i></br>
</form>

jQuery/javascript:

$(document).ready(function(){
    $('form').submit(function(){
        var sig = $('#sig').val();
        sig_md5 = $.md5(sig);
        $('#sig').val(sig_md5);
    });
});
like image 95
cssyphus Avatar answered Sep 16 '25 13:09

cssyphus


You can use getter if you don't care about IE<=9

var data = {}
Object.defineProperty(data, 'signature', { 
   get: function() { return document.getElementById('sig').value; },
   set: function(value) { /* set input value hereif needed */ }
})

read more on MDN

like image 43
Ilya Novojilov Avatar answered Sep 16 '25 13:09

Ilya Novojilov