views:

537

answers:

2

Hi,

I have below html code

<div id="divTest">
    Hello
    <input type="text" id="txtID" value="" />
    <input type="button" onclick="getForm();" value="Click" />
</div>

And I have below javascript function to get innerHtml value

 <script language="javascript" type="text/javascript">
    function getForm()
    {
        var obj = document.getElementById('divTest');
        alert(obj.innerHTML);
    }

    </script>

I am trying to get the complete innerHtml value. When user put some value in "txtId" and when he clicks on button it should show all the innerHtml with txtId value in it. It is working fine in Internet Explorer but failing in Mozilla.

Please suggest what type of changes I need to do in javascript function or I need some Jquery for this.

Thanks.

Best Regards,

A: 

I've run across this myself - try using obj.innerHtml (note capitalisation) instead.

Ian Kemp
its giving "undefined" after using obj.innerHtml. Please suggest
MKS
The proper case is infact `innerHTML`.
Crescent Fresh
A: 

Hi Guys,

I solved my problem by using below jQuery

 <script type="text/javascript">   



$(document).ready(function() 



 {

  var oldHTML = $.fn.html;



  $.fn.formhtml = function() {

    if (arguments.length) return oldHTML.apply(this,arguments);

    $("input,textarea,button", this).each(function() {

      this.setAttribute('value',this.value);

    });

    $(":radio,:checkbox", this).each(function() 

    {



      if (this.checked) this.setAttribute('checked', 'checked');

      else this.removeAttribute('checked');

    });

    $("option", this).each(function() 

    {      

      if (this.selected) this.setAttribute('selected', 'selected');

      else this.removeAttribute('selected');

    });

    return oldHTML.apply(this);

  }; 

  $.fn.html = $.fn.formhtml;

});

$(document).ready(function() 

{ 

    $('input[type=button]').click(function() {

        alert($('#divTest').html());

    });

});

    </script>

and the html was as below:

<div id="divTest">

    Hello

    <input type="text" id="txtID" />

    <input type="text" id="txtID" />

    <input type="text" />

    <input type="text" id="Text1" />

    <input type="text" id="Text1" />

    <input type="text" />

    <input type="text" />

    <input type="text" />

    <input type="text" id="Text5" />

    <input type="text" id="Text6" />

</div>

<input type="button" value="Click" />
MKS
You shouldn't have duplicate IDs
fudgey