1

The question is as simple as - is it possible to create a user-defined method for standard js objects like the val method for input ? That is, to do smth like:

function my_new_method()
{
   ...
}

...
alert($('td input').my_new_method())
3

2 Answers 2

3

Since that is looks like jQuery (not vanilla/standard JavaScript), you can simply use $.fn.my_new_method = // your function

You can read up on how to create a jQuery plugin here: https://learn.jquery.com/plugins/basic-plugin-creation/

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Object prototype.

Object.prototype.my_new_method = function() {
        console.log(this.val());
    }

$("#test").my_new_method();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' value='abc' id="test">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.