So i tried to modified the original jQuery's val() method using the following code
(function ($) {
var original_val = jQuery.fn.val;
jQuery.fn.val = function( value ) {
var elem = this[0], val = undefined;
// Set for value first, if its undefined
val = value ? value : "";
if (elem){
if (elem.hasAttribute('thisattribute')){
if (val){
if (typeof val === 'number'){
// Do something here if val is a typeof number
}
} else {
// Do something here if val doesn't exist
}
}
}
console.log("Let me see what is value ::: ", val);
console.log("Let me see what is elem ::: ", elem);
return original_val.apply(this, [val]);
}
})(jQuery);
Using the code above, i check if the input element has a certain attribute on it, then proceed with modifying the value, before passing it to the original jQuery's val() method.
Using this method i managed to modify the value when i use the following code
$(id).val(10000)
But when i tried to retrieve the value using bottom code, it failed
$(id).val()
Some more, i can no longer chain the val() method with other method like trim() after i modified it, as it throws me the following error
Uncaught TypeError: input.val(...).trim is not a function
What did i do wrong here?