javascript - Set value on different html element types with same jQuery function -


using cordova 2.5.0, i'm using following function go <input> elements within html page.

function getinput() {    var formelements = new object();    $("form :input").each(function(){        formelements[this.name] = $(this).val(); //that's line    });    return formelements; } 

actually, don't "get" elements, rather values, can see in fourth line of snippet.

now, i'm trying set values later on in application.

function insertresults(data) { //data json string     console.log("in insertresults (js): " + data);     var jso = json.parse(data);     (var key in jso) {         addcontenttoelement(key, jso[key]);         } }  function addcontenttoelement(elementname, valuetoset) {     $(function() {         $("input[name='" +elementname +"']").val(valuetoset);     }); } 

the console.log() prints this: in insertresults (js): {"checkbox3":"","textfieldmulti1":"some text","textfield1":"some more text","boxed1":"even more text","checkbox1":"on","checkbox2":"on"}

and above code works fine html textfields. however, if possible, i'd set value checkboxes et cetera well, , kinda hoped .val(value) trick, after all, can retrieve value of element .val(), other elements, checkbox. i'd rather not check every element want add content to, , according element type, use correct jquery method set value of element.

any way or method achieve this?

(to clarify little more:) addcontenttoelement("checkbox3", "on"); not work, although retrieve on .val() element, addcontenttoelement("textfield1", "some text") does. , know can .prop('checked', true), or .attr('checked', true), since can value .val(), why not set it?

you cannot use "val()" checkboxes , radioboxes check because different textboxes. works when change "checked" attribute send form data server, website,..etc. can use val() when want change value (value attribute).

so if understand correctly, want change checkboxes when "addcontenttoelement" function "on" value. changed code , can use code below.

function addcontenttoelement(elementname, valuetoset) {  $(function() {      if (valuetoset === "on") {        $("input[name='" +elementname +"']").attr('checked', true);      } else {        $("input[name='" +elementname +"']").val(valuetoset);      }  }); } 

but should send type function find input checkbox.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -