Hi I want to implement into my plugin a function inside an object.
The goal is:
I have many input with a default value.
When I click on It if the value is “default value” cancel the text inside. And this it works.
The problem is now that:
When I submit the form I call a function that check parameter and I want that if the input type have the default value cancel the value. But now only the last element do that and not all object. I have create a function inside the main function of the plugin. How to check every object inside the plugin when submit and check if has the default value or not?
my plugin:
(function($){
$.fn.extend({
contact: function(options) {
var defaults = {
}
var options = $.extend(defaults, options);
return this.each(function() {
var o = options;
var obj = $(this);
var value_base = obj.val();
obj.click(function(){
if(obj.val()== value_base){
obj.val("");
}
});
obj.blur(function() {
if (obj.val() == ''){
obj.val(value_base);
}
});
$.fn.afterSubmit = function(options) {
if(obj.val()== value_base){
obj.val("");
}
};
});
}
});
})(jQuery);
the code inside my page
<script type="text/javascript">
$(document).ready(function() {
$('.contact').contact();
});
function autoriz(){
$('.contact').afterSubmit();
return false;
}
</script>
<form method="post" action="contatti.php" onSubmit="return autoriz();" enctype="multipart/form-data" name="contatti">
<input type="text" name="name" class="contact" value="Name" id="name"/>
<input type="text" name="surname" class="contact" value="Surname" id="surname"/>
</form>
Take a look at this fiddle http://fiddle.jshell.net/YskZy/
In this example we bind the event submit passing the form id, so before the form is submitted, we can clear the default values