All i want to do is to check if the textfield has been changed. if not, i want to highlight the boxes when submit is pressed. how do i do that? seems very simple, but not sure why every other solution is so complicated
ooo! New version! 1.7 is jQuery 1.42 compatible.
Mark Stahler
2010-03-17 15:38:36
+1
A:
Bassicly, you just say it has never been changed, and when it cahnges, you set a flag saying is has been changed:
var changed = false;
$('#textfield').change(function(){
changed = true;
});
if(changed){
$('.textbox').each(function(){
$(this).addClass('.highlighted');
//or something like this, whatever you want to do to highlight them
});
}
Pim Jager
2010-03-16 00:18:13
+4
A:
Building upon Pim's answer, you can associate the changed
flag for each text field using jQuery's data API.
// initially, assign changed to false for all text fields
$("input:text").data("changed", false);
// if any field changes, set its changed flag to true
$("input:text").change(function() {
$(this).data("changed", true);
}
// finally on submission, get all text fields that did not
// change by checking their "changed" property
var unchangedItems = $("input:text").filter(function() {
return $(this).data("changed") === false;
});
Anurag
2010-03-16 00:27:04
be aware that changing an input's value with javascript doesn't fire the `changed` event
Andrew Bullock
2010-03-17 12:58:42
@Andrew Bullock - yes, but you CAN put a $('fieldIChanged').trigger("change"); on it when you DO change it that way :) where 'fieldIChanged' is the selector for that item (this?)
Mark Schultheiss
2010-03-17 13:06:32
A:
DEMO: http://jsbin.com/aceze/29
$(function() {
$('#myform').submit(function(){
var error = false;
$('#myform > .validate').each(function(){
if ( this.value == 'fill this input' || this.value == '') {
$(this).css('border','1px solid red');
error = true;
} else {
$(this).css('border','1px solid green');
}
});
if ( error == false ) {
// success
} else {
// error
}
return false;
});
});
HTML
<form id="myform" methot="post" action="#">
<input class="validate" type="text" value="fill this input" name="input"/> <br />
<input type="submit" value="submit" name="submit"/>
</form>
Give each Input you want validate the .validate class!
aSeptik
2010-03-16 02:23:58
`this.value == ''` is equal to `this.value == 'some text'` assuming each input have `some text` as value! althoght this is a proof of concept and it is what the OP asked, check after submission not onChange!!!
aSeptik
2010-05-04 13:37:11