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

Asked

Viewed 140 times

1

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

One has to be updated value and another 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 it the way up but it’s not working

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 just wasn’t clear why this other one if within the else. But this way checking tag name already solves whether to use .val() or .html().

  • As I was not sure that could generate error I decided to put another if, I will use your example

  • Blz. Give feedback on whether or not you’ve solved.

  • DVD worked, thanks a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.