How do I know if an element has the html or value property?

1

I have two elements that need to be updated a DIV and another INPUT

One has to be updated value and another the html, example

...
    $.each(json, function (index, value) {
       if (typeof $("#" + index).val() !== "undefined") {
           $("#" + index).val(value);
       } else {
           if (typeof $("#" + index).html() !== "undefined") {
               $("#" + index).html(value);
           }
      }
   });
...

I'm doing the above but it's not working

    
asked by anonymous 23.05.2018 / 01:44

1 answer

2

You can check by tag name:

...
    $.each(json, function (index, value) {
       if ( $("#" + index).prop("tagName") == "INPUT") {
           $("#" + index).val(value);
       } else {
           $("#" + index).html(value);
      }
   });
...

It's just not clear why this other if within else . But this way verifying tag name already solves whether to use .val() or .html() .

    
23.05.2018 / 01:59