0

I have this js....

$("#form").submit(function(){  
var answerlist = "first";  
var answerlist = answerlist + "," + $("#get_answer").attr("value");  
alert(answerlist);  
});

i'm trying to add the value of the input field with the id #get_answer to the answelist variable....

i expect that the answerlist is now = first move // where the value of input field is move and yes the result is a success...

and now for the second time around i'm trying to add another value to the answerlist variable....

the value is on
i expect that the outcome will be first move on

but when i check using the alert() it returns first on

can someone help me plsss??

what i want is to increment the value(string) to the variable answerlist

like

answerlist = "first";
answerlist = "first move";
answerlist = "first move on";
0

5 Answers 5

3
(function () {
    // to isolate it from rest of the code 

    var handler = function (target) {
        var list = 'first';
        return function () {
            list = list + target.value;
            alert(list);
        };
    },
    action = handler(document.getElementById('get_answer'));

    $(document.getElementById('form')).submit( action );

}());

This structure is called "closure", its one of the most important features in JavaScript language and you should learn how to use it. This lecture might help.

And please stop using $.attr() for trivial things.

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

Comments

2

With the snippet you have here, you could never have more than "first, move" since you are setting answerlist ever time on the 2nd line, and it is only generated when the form is submitted. So unless the value of your input was "move on" it would never show the expected result.

You could put the answerlist variable outside of the function scope if you want to, and add the values whenever the input is changed...

var answerlist = "first";  
$("#form #get_answer").change(function(){  
    answerlist = answerlist + "," + $("#get_answer").val();  
});
$("#form").submit(function(){  
    alert(answerlist);  
});

However, note you won't be able to remove anything from this list. Instead, you would want to use an array and push/pop the values if you need to have the potential of removing items. JavaScript Array Ref

1 Comment

i found the problem sir.. with the help of @mightyuhu.... all i just need to do is to put the answerlist variable outside the function... but still thank you sir for your help..
1

You are initializing the value of the variable inside the function, so naturally, when it's called once again the value will return to first

Initialize it outside the function.

2 Comments

thanks sir.... you made me understand... thank you verymuch... i forgot to place the variable answelist outside the function....
This wont work as expected, if same variable is already elsewhere. JavaScript uses function-level scoping, so this initialized variable of yours will most likely end up in global scope. Basically the problem is same as with global variables in any other language: unintended consequences.
1

This should do it

$("#form").submit(function(){  
   var answerlist = "first";  
   var answerlist = answerlist + "," + $("#get_answer").val();  
   alert(answerlist);  
});

EDIT:

DEMO

4 Comments

updated the demo with minor changes, your version should may be look similar to that
someone already solved it for me sir.... that also works by appending the value in a div.... but for me it's more comfortable to put it in a variable sir...
@Dhiraj the problem is that he's initializing the variable inside the function. In this particular case .val() would be my preferred way of retrieving the value since it's shorter but .attr("val") works equally well.
yes indeed, my demo is similar to OP's requirement(I just now edited my demo even with a form submit), please correct me if im wrong
0

Try this...

var answerlist = answerlist + "," + $("#get_answer").val();  

1 Comment

declare variabe answerlist before function!! var answerlist = "first";

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.