Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, mabey somebody can Help I try to make this code with the example code to work, however i it does nothing and I asking myself what do i do wrong.

The code should make the input border red or green, when somebody enters a word that match to the datalist, or not (then it is red)

let btn = $("#btnSend");
let form = $("#zeForm");
let input = $("#zeInput");
let msg = $("#msg");
let allowedValues = ["atown", "btown", "ctown"]; // same values as the options in your datalist


btn.on('click' , function() {
    let allGood = false;

    allowedValues.each(function(index, element) {
        if (element === input.value) {
            allGood = true;
            return;
        }
    })

    if (allGood) {
        msg.text("Success!!");
        msg.attr('style',"color:green");
        //form.submit();
    } else {
         msg.text("This value is not accepted";
         msg.attr('style',"color:red");
    }
   msg.attr('style',"display:inline");

});



The html code for test:
<pre lang="HTML">
<pre><form id="zeForm">
    <input id="zeInput" type="text" list="typ" name="name" placeholder="gtown" > 
        <datalist id="typ">
            <option value="atown">atown</option> 
            <option value="btown">btown</option> 
            <option value="ctown">ctown</option> 
        </datalist> 
    </input>
    <p id="msg"></p>
    <button id="btnSend" type="button">send</button>
  </form>


What I have tried:

I had before an javaskript code which I try to translate in jquery, however until now the jquery code does not work with the example, but the javaskript code does. see:

How to convert this javascript code to jquery[^]
Posted
Updated 6-Jun-18 6:18am
Comments
Member 13812021 5-Jun-18 11:10am    
I guess i am wondering where the matching data comes from? Database? Also where are they inputting this? You show a dropdown list but not an entry textbox.
ThilinaMD 6-Jun-18 9:48am    
Have you linked jquery.js file?

1 solution

Several errors in your code:

Datalist
The <datalist> cannot be nested inside the <input>:
HTML
<input id="zeInput" type="text" list="typ" name="name" placeholder="gtown" />
<datalist id="typ">
    <option value="atown">atown</option> 
    <option value="btown">btown</option> 
    <option value="ctown">ctown</option> 
</datalist>

Each
You can't use allowedValues.each(fn); you have to use $.each(allowedValues, fn):
JavaScript
$.each(allowedValues, function(index, element) {
jQuery.each() | jQuery API Documentation[^]

Value
The jQuery object doesn't contain a property called value; you need to use the val() method instead:
JavaScript
if (element === input.val()) {
.val() | jQuery API Documentation[^]

Also, calling .attr("style", "...") will overwrite the element's style. To change an individual style, use the css() method[^] instead:
JavaScript
msg.css("color", "green");
...
msg.css("color", "red");
...
msg.css("display", "inline");

Demo[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900