Jump to content
Sign in to follow this  
Maheshwaran

DataList update from jquery

Recommended Posts

My idea is change the datalist based on Textbox.

# when textbox is empty, I want to display "initial,always"

# when textbox is select "initial or awlays" and datalist should show "LHS,RHS".

# again the textbox is empty, I want to show "initial,always".

 

Here the second scenario is showing only "initial" and when Textbox empty it showing "initial,always,LHS,RHS".

can you please give me some hint?

 

<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        JQuery | Set value of input text.
    </title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
    </script>
 
</head>
 
<body style="text-align: center;">
    <input id="input" list="anrede" />
    <datalist id="anrede">
    </datalist>
    <br>
    <br>
    <script>
        var mycars = new Array();
        var block = new Array();
        mycars[0] = 'initial';
        mycars[1] = 'always';
        block[0] = 'LHS';
        block[1] = 'RHS';

        var options = '';

        $("#input").click(function(event) {
            $('#anrede').empty();
            var option = '';
            if ($('#input').val()=='') {
                for(var i = 0; i < mycars.length; i++)
                    options += '<option value="'+mycars+'" />';
                    $('#anrede').append(options);
            } else if ($('#input').val()=='initial') {
                for(var ii = 0; ii < block.length; ii++) {
                    options += '<option value="'+block[ii]+'" />';
                    $('#anrede').append(options);
                }
            }
        });
    </script>
</body>
 
</html>

Share this post


Link to post
Share on other sites

For the default value, you should simply add value="yourdefaultvalue" to your input tag. 

What do you want to display "LHS,RHS" in your code, when datalist is set to "initial or awlays"

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...