JSFiddle - React, Tailwind, and code Playground
HTML
<form id="frm">
<input name="t1" type="text" value="test 1"/>
<input name="t2" type="text" value="test 2"/>
<select name="myoptions">
<option>1</option>
<option selected>2</option>
<option>3</option>
</select><input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
<textarea name="fff">asdasdas</textarea>
</form>
<button onclick="alert(TestDirty());">GO</button>
JavaScript
function TestDirty(){
var formDirty = false;
$('#frm :input').each(
function(){
if($(this).is("input"))
{
//alert('Name :' + $(this).attr('name') + ">" +$(this).val()+ " != " + $(this)[0].defaultValue);
var type = $(this).attr('type');
if (type == "checkbox" || type == "radio")
{
if ($(this).is(':checked') != $(this)[0].defaultChecked)
{
//alert('Form dirty by' + $(this).attr('name') + ">" +$(this).is(':checked')+ " != " + $(this)[0].defaultChecked);
formDirty = true;
}
}
else if (type == "hidden" || type == "password" || type == "text")
{
if ($(this).val() != $(this)[0].defaultValue)
{
//alert('Form dirty by' + $(this).attr('name') + ">" +$(this).val()+ " != " + $(this)[0].defaultValue);
formDirty =true;
}
}
}
else
{
var type = $(this).get(0).tagName;
if (type == "TEXTAREA")
{
if ($(this).val() != $(this)[0].defaultValue)
{
//alert('Form dirty by' + $(this).attr('name') + ">" +$(this).val()+ " != " + $(this)[0].defaultValue);
return formDirty;
}
}
else(type == "SELECT")
{
alert('Form dirty by ' + $(this).attr('name') + ">" +$(this).val()+ " != " + $(this)[0].defaultSelected);
if ($(this).val() != $(this)[0].defaultSelected)
{
//alert('Form dirty by' + $(this).attr('name') + ">" +$(this).val()+ " != " + $(this)[0].defaultValue);
return formDirty;
...