Julian Paolo Dayag
Julian Paolo Dayag

Reputation: 3719

How to add a string to a variable;

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";

Upvotes: 0

Views: 811

Answers (5)

tereško
tereško

Reputation: 58444

(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.

Upvotes: 3

Dhiraj
Dhiraj

Reputation: 33618

This should do it

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

EDIT:

DEMO

Upvotes: 1

jkriddle
jkriddle

Reputation: 708

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

Upvotes: 2

Icarus
Icarus

Reputation: 63956

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.

Upvotes: 1

Vaishu
Vaishu

Reputation: 2363

Try this...

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

Upvotes: 0

Related Questions