-2

I'm writing a code where there is a string to be compared with the variable array. Here if it is found, I need to alert that the match is found. Below is my code.

Here I'm comparing a single string with the array of strings. I'm not comparing two different js arrays.

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />

    <script type="text/javascript">
        function addTable() {
            var dataTerm = document.getElementById('Select2').value;
            var cancellations = new Array();
            var changeInfo = new Array();
            var idq = new Array();
            var others = new Array();
            var replace = new Array();
            var moreInfo = new Array();
            var salesRep = new Array();
            var custReq = new Array();
            var myTableDiv = document.getElementById("myDynamicTable");
            myTableDiv.border = "1";


            var variablesArray = new Array[cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq];

            for (var i = 0; i < variablesArray.length; i++) {
                if (dataTerm == variablesArray[i].value) {
                    alert("matched at" + variablesArray[i].value);
                }
            }
        }
    </script>

</head>
<body>

    <table class="auto-style1">
        <tr>
            <td class="auto-style3">Renewal/Product #</td>
            <td class="auto-style4">
                <input id="Text1" type="text" /></td>
            <td class="auto-style5">Product Title</td>
            <td>
                <input id="Text2" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Account #</td>
            <td class="auto-style4">
                <input id="Text3" type="text" /></td>
            <td class="auto-style5">Product Code</td>
            <td>
                <input id="Text4" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Invoice #</td>
            <td class="auto-style4">
                <input id="Text5" type="text" /></td>
            <td class="auto-style5">Suspended Date</td>
            <td>
                <input id="Text6" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Shipment / To #</td>
            <td class="auto-style4">
                <input id="Text7" type="text" /></td>
            <td class="auto-style5">Term Inc/ Dec</td>
            <td>
                <input id="Text8" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Tracking #</td>
            <td class="auto-style4">
                <input id="Text9" type="text" /></td>
            <td class="auto-style5">Qty Inc/ Dec From</td>
            <td>
                <input id="Text10" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Unit #</td>
            <td class="auto-style4">
                <input id="Text11" type="text" /></td>
            <td class="auto-style5">Qty Inc/ Dec To</td>
            <td>
                <input id="Text12" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Billing Address</td>
            <td class="auto-style4">
                <input id="Text13" type="text" /></td>
            <td class="auto-style5">E-mail ID</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style3">Name</td>
            <td class="auto-style4">
                <input id="Text14" type="text" /></td>
            <td class="auto-style5">Request Type</td>
            <td>
                <select id="Select2" name="D2">
                    <option value="" disabled selected>Select your option</option>
                    <option value="Cancellations">Cancellations</option>
                    <option value="Changing Information">Changing Information</option>
                    <option value="Increase or Decrease Quantity/Term/Users">Increase or Decrease Quantity/Term/Users</option>
                    <option value="Others">Others</option>
                    <option value="Replacement">Replacement</option>
                    <option value="Fore more information">Fore more information</option>
                    <option value="Contacting Sales rep">Contacting Sales rep</option>
                    <option value="Requesting Customer">Requesting Customer</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class="auto-style3" colspan="4">
                <input id="Button1" type="button" value="Click Me" onclick="addTable()" /></td>
            <td class="auto-style4" colspan="4">&nbsp;</td>
            <td class="auto-style5" colspan="4">&nbsp;</td>
            <td colspan="4">&nbsp;</td>
        </tr>
    </table>

    <br />
    <br />
    <br />
    <br />
    <div style="height: 420px" id="myDynamicTable">
    </div>

</body>
</html>

Here in my case, to my surprise nothing happens when I click on the button.

user3872094
  • 3,269
  • 8
  • 33
  • 71
  • 2
    Possible duplicate of [Comparing Arrays of Objects in JavaScript](http://stackoverflow.com/questions/27030/comparing-arrays-of-objects-in-javascript) –  Jan 29 '16 at 15:33
  • it's `new Array()`, not `new Array[]`... Maybe you should open your console and clear all the syntax errors in the code – baao Jan 29 '16 at 15:34
  • Why do you need a loop of `variablesArray` in the first place? You already have the data for the array to test against without needing a loop – charlietfl Jan 29 '16 at 15:38

2 Answers2

0
var cancellations = new Array();

Here you create a (new) empty array.

var variablesArray = new Array(cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq);

Here you create an array containing several empty arrays.

if (dataTerm == variablesArray[i].value)

The expression variablesArray[i] will evaluate to one of the empty arrays that you put in this array. The expression .value will evaluate to undefined because arrays don't have an attribute named value. Since the string you have is not equal to undefined, the condition is never true.

dsh
  • 12,037
  • 3
  • 33
  • 51
0

Take a look: when you get dataTerm, it's a string value. You are comparing dataTermwith an element in variablesArray, these elements are Arrays (once you initialized then with new Array(). You can't compare so different things.

Another topic: don't use square brackets in

new Array[cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq];

Array is a function, use parenthesis.

new Array(cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq);

Try this in the loop:

for (var i = 0; i < variablesArray.length; i++) {
    for(j = 0; j < variablesArray[i].length; j++){
        if (dataTerm == variablesArray[i][j].value) {
            alert("matched at" + variablesArray[i][j].value);
        }
    }
}
André Claudino
  • 558
  • 4
  • 18