Home >Web Front-end >JS Tutorial >How Can I Effectively Clear an `` Element's Value Using jQuery?
Clearing Using jQuery
Attempting to clear an control value with jQuery using the following code:
$('#control').attr({ value: '' });
may prove futile. However, a viable solution presents itself: wrap the element in a form, activate the reset function on said form, and subsequently detach the form using .unwrap().
window.reset = function(e) { e.wrap('<form>').closest('form').get(0).reset(); e.unwrap(); }
This approach has proven effective across Opera, Firefox, Safari, Chrome, and IE6 , extending to various form element types (excluding type="hidden").
To visualize the utility of this method, consider the following illustration:
<form> <input>
Clicking either button will reset the respective form element, demonstrating the versatility of this technique.
The above is the detailed content of How Can I Effectively Clear an `` Element's Value Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!