0

I am writing javascript that splits an array based on delimiter(,) and puts that value to text box. Each array contains exact five comma separated values. I tried the below code and getting error

"Error: Object doesn't support this property or method".

I found the information related to this error in this web site but it appears that it is not applicable to this context. Please anyone help me in fixing this issue.

<script>

function show() {
        var mycars = new Array();
        mycars[0] = "768,232,574,768.234";
        mycars[1]  = "abc, def, ghi, jkl,mno";

        for(var i=0; i<mycars[i].length; i++){
            mycars= mycars.split(",");

            document.getElementById('name')[0].value=mycars;
        }

    }
</script>
<form>
    <table class="cmn-table" id="t1" border="1"
        style="margin-left: 0.2em; margin-right: 0em">
        <tr>
            <th>Sl.No</th>
            <th>IP</th>
            <th>DP</th>
            <th>TCP</th>
            <th>SD</th>
            <th>ED</th>
        </tr>
        <tr>
            <td><input type="text" name="name11" id="s1" value="1" size="2"
                readonly="readonly" /></td>

            <td><input type="text" name="name0" id="name" size="20">
            </td>
            <td><input type="text" name="name1" id="name1" size="20">
            </td>
            <td><input type="text" name="name2" id="name2" size="20">
            </td>
            <td><input type="text" name="name3" id="name3" size="15">
            </td>
            <td><input type="text" name="name4" id="name4" size="15">
            </td>
        </tr>
        <tr>
            <td><input type="text" name="name0" id="s2" size="2" value="2"
                readonly="readonly" /></td>
            <td><input type="text" name="name" id="name5" size="20">
            </td>
            <td><input type="text" name="name1" id="name6" size="20">
            </td>
            <td><input type="text" name="name2" id="name7" size="20">
            </td>
            <td><input type="text" name="name3" id="name8" size="15">
            </td>
            <td><input type="text" name="name4" id="name9" size="15">
            </td>
        </tr>
    </table>

</form>
<input type="button" value="Click" onclick="show()">

5
  • 2
    getElementById() returns one result. There's no need fr the array subscript. Commented Jul 2, 2013 at 18:41
  • 1
    You're attempting to overwrite your array here mycars= mycars.split(","); Commented Jul 2, 2013 at 18:43
  • Are you trying to split the comma delimited string over the text inputs of each row? Commented Jul 2, 2013 at 18:44
  • @j08691 Yes to your question Commented Jul 2, 2013 at 18:46
  • I read the website before posting the question and tried my best to explain. Technically I may wrong and this is the reason I am here. May I know the reason for -ve mark for this question? Commented Jul 2, 2013 at 18:48

1 Answer 1

1

You have to make one small change to your HTML (change the id of "name" to "name0") and use this JavaScript:

function show() {
    var mycars = new Array();
    var counter = 0;
    mycars[0] = "768,232,574,768,234";
    mycars[1] = "abc, def, ghi, jkl,mno";
    for (var i = 0; i < mycars.length; i++) {
        foo = mycars[i].split(",");
        for (var j = 0; j < foo.length; j++) {
            document.getElementById('name' + counter).value = foo[j];
            counter++;
        }
    }
}

jsFiddle example

Note that I'm assuming you have a typo in your example here mycars[0] = "768,232,574,768.234"; and that it should be mycars[0] = "768,232,574,768,234";

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

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.