1

The code looks like this

function Scripts() {this.FindById = function (id) {
    this.FindById.constructor.prototype.value = function () {
        return document.getElementById(id).value;

    }}}

var Control = new Scripts();

Now when i say Control.FindById("T1").value(). I am not able to get the textInput("T1")'s value.

0

2 Answers 2

1

It seems that your code is a bit more complicated then it should be ;-)

Personally I would write it this way (not tested):

function Scripts() {
  this.findById = function(id) {
    var el = document.getElementById(id);

    return {
      value: function() { 
        return el.value;
      }
    }
  }
}

The findById() now closes over a node and returns an interface that can return its value.

Also, your idea sounds a lot like Singleton, so you wouldn't even need the extra Scripts constructor:

var Control = {
    findById: function(id) {
        var el = document.getElementById(id);

        return {
            value: function() { 
                return el.value;
            }
        }
    }
}

Working example: http://jsfiddle.net/YYkD7/

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

5 Comments

Sorry my mistake it is return document.getElementById(id).value. I dont know how "=" came in between
@Rakesh sorry to hear that, but here's a working fiddle: jsfiddle.net/YYkD7
sorry, it was working fine just that i dint check it properly.Thanks for the help man. I have on more question. How can i have more methods similar to "value" inside "FindById". Something like this var Control = { findById: function(id) { var el = document.getElementById(id); return { value: function() { return el.value; } } } }
@Rakesh You can just add your function after the value: function() { ... } definition.
Thanks mate, found out it has to be comma separated :).
0

Try this:

function Scripts() {this.FindById = function (id) {
    this.FindById.constructor.prototype.value = function () {
        return document.getElementById(id).value
    }}}

You didn't close the last "}" :-)

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.