0

I'm wondering if this is at all possible to do...

Essentially I'm storing information of the form (i.e. select, input, textarea) within a json object.

var inputs = {
    "name": {
        errorMsg: "Please enter a name!",
        inputName: "name",
        inputType: "input"
    }
}

From there, I want to construct the input value with jQuery such as:

$('input#name').val();

and have it actually return the value of the input field.

I was thinking of maybe doing something like this:

var inType = inputs.name.inputType;
var inName = inputs.name.inputName;
var stringType = $("'" + inType + "#" + inName + "'");
alert(stringType.val());

But that gives me an undefined value.

Here is the html I'm using:

 <input name="name" id="name" value="test" />

I've noticed that if I add add to the json object: value: $('input#name').val() after inputType and then go alert(inputs.name.value) it gives me the proper value. So should I construct the string and then prop it to the my inputs object?

2 Answers 2

1

var stringType = $("'" + inType + "#" + inName + "'");

The quotes are not necessary. this should be:

var stringType = $(inType + "#" + inName);

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

1 Comment

I was staring at that for way too long. Thanks :)
0

use this:

var inType = inputs.name.inputType;
var inName = inputs.name.inputName;

var selector = inType + '#' + inName ;
var stringType = $(selector);

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.