StudentRik
StudentRik

Reputation: 1049

Get value from DOM at run time

I am trying to get a value from a textbox and I'm only returning [object Object].

var key = $('body').on('click', '#ReportReferenceElectrical',function() {
    $('#ReportReferenceElectrical').val();
});
var REPORTS_KEY = key;

I have tried the usual:

var key = $('#ReportReferenceElectrical').val();

The JS is in an iife and the value needs to retrieved when I submit the form to save but using this way its empty as the value was empty when the form loaded, I though the top code snippet might return me the value, but not.

Upvotes: 0

Views: 44

Answers (1)

Amit Joki
Amit Joki

Reputation: 59262

The callback will get executed only when it is triggered. In this case, manually $.fn.triggering isn't an option, I suppose as it will be ""(Empty)

What you need is a callback.

function callback(key) {
   alert(key);
   // do something with the key now
}

And then call it in the event handler. I'd suggest going for the blur event rather than click

$('body').on('blur', '#ReportReferenceElectrical',function() {
    callback($('#ReportReferenceElectrical').val());
});

Upvotes: 1

Related Questions