2

I have an array ["A", "B", "C"] and I want to show the elements of the array in a text box but without comma. They will appear in the text box as like: ABC

function addtoFormula(){
    var a = ["A", "B", "C"];
    $.each([a], function( index, value ) {
        var putvalue = value;
        $("#formula").val(putvalue);
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" name="formula" id="formula">
<button onclick="addtoFormula();">Add</button>

3 Answers 3

3

You do not need to loop through the the array at all. Just use Array.prototype.join() with empty string ('') as the separator:

function addtoFormula(){
    var a = ["A", "B", "C"];
    $("#formula").val(a.join(''));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" name="formula" id="formula">
<button onclick="addtoFormula();">Add</button>

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

Comments

2

You can simply achieve this by

function addtoFormula() {
   var a = ["A", "B", "C"];
    $("#formula").val(a.join(''));
}

Comments

0

You can simply use the join method of the array to convert the array into a string

function addtoFormula(){
    var a = ["A", "B", "C"];
    // Note that join accept the join delimiter. Here I have 
    // space, but you can add what ever you need.
    $("#formula").val(a.join(''));     // Output: ABC
    $("#formula").val(a.join(' '));    // Output: A B C
    $("#formula").val(a.join(','));    // Output: A,B,C
    $("#formula").val(a.join(', '));    // Output: A, B, C
}

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.