Skip to content Skip to sidebar Skip to footer

Get Value From Multiple Input And Search Array Match (with And E Or)

In the function onclilck, I have to get the value from each input and search with that value, a match in my array and then report in DIV 'result'. The problem is that I have to do

Solution 1:

you can do this:

  1. Copy the pharmacies array to a temp array.
  2. Get all <input> element.
  3. Search among <input> elements for given value.
  4. Delete array element, if given value not found.
  5. Remove undefined values from Temp array.
  6. Print the temp array.

Result:

var pharmacies = [
    [ 'Vaccaro', '', 'Bagheria', '90011' ],
    [ 'Greco', '', 'Bagheria', '90011' ],
    [ 'Timoneri', '', 'Bagheria', '90011' ]
];

functionsearch() {
    var tempArr = pharmacies.slice( 0 ),
        elem = document.getElementsByClassName( 'campi' );

    for ( let i = 0; i < elem.length; ++i ) {
        var value = elem[ i ].valueif ( value ) {
            for ( var j = 0; j < tempArr.length; ++j ) {
                if ( tempArr[ j ] != undefined ) {
                    if ( tempArr[ j ].indexOf( value ) == -1 ) delete tempArr[ j ]
                }
            }
        }
    }

    tempArr = tempArr.filter( Boolean );
    printResult( tempArr )
}

functionprintResult( arr ) {
    if ( arr.length ) {
        var result = '<table><tr><th>#</th><th>Nome</th><th>Indirizzo</th><th>Città</th><th>Cap</th></tr>';

        for ( var x in arr ) {
            result += '<tr><td>' + (+x + 1) + '</td>'for ( var y in arr[ x ] ) {
                result += '<td>' + arr[ x ][ y ] + '</td>'
            }
            result += '</tr>'
        }

        result += '</table>'
    } else {
        result = 'No data found'
    }

    document.getElementById( 'result' ).innerHTML =  result
}
input {
    margin: 5px
}
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%
}

td, th {
    border: 1px solid #ccc;
    text-align: left;
    padding: 8px
}

tr:nth-child(even) {
    background-color: #ddd
}
<inputclass="campi"id="nome"type="text"name="name"placeholder="nome"><inputclass="campi"id="indirizzo"type="text"name="address"placeholder="indirizzo"><inputclass="campi"id="città"type="text"name="city"value="Bagheria"placeholder="città"><inputclass="campi"id="cap"type="text"name="zip"placeholder="cap"><br><buttonid="search"type="submit"onclick="search()">Cerca</button><br><br><divid="result"></div>

Post a Comment for "Get Value From Multiple Input And Search Array Match (with And E Or)"