Fred J.
Fred J.

Reputation: 6019

set value of input text in code

This code is trying to set the value of an input text box when the function restoreFromSerialize is called inside the template helper.
It is not inserting elementValue in the input text box even though it's value has been verified in the browser console when console.log(elementName+" "+elementValue); is fired. Thanks

function restoreFromSerialize() {
  formValues.split('&').forEach(function (doc) {
    var elementName = doc.split('=')[0];
    var elementValue = doc.split('=')[1];
    if($('[name=elmentName]').attr('type') == 'radio') {
      $('[name=elementName]').prop('checked', true);
    } else {
      console.log(elementName+" "+elementValue);
      $('[name=elementName]').val(elementValue); //<<<<< NOT WORKING
    }
  })
}

Upvotes: 0

Views: 60

Answers (1)

Manish Jangir
Manish Jangir

Reputation: 5437

Re-factoring your entire code:

function restoreFromSerialize(formValues) {
  formValues.split('&').forEach(function (doc) {
    var splited = doc.split('=');
    var elementName  = splited[0];
    var elementValue = splited[1];
    if($('[name="'+elementName+'"]').is(':radio')) {
      $('[name="'+elementName+'"]').prop('checked', true);
    } else {
      console.log(elementName+" "+elementValue);
      $('[name="'+elementName+'"]').val(elementValue); //<<<<< NOT WORKING
    }
  });
}

The reason your code was not working was, you needed to pass the variable in the selector not the string literal.

Upvotes: 2

Related Questions