-1

Recently i started working on a project, in this i have to add the text entered by user in a text box into a ul list in bootstrap. This script is in my head section

<script type="text/javascript">
        function addNames(){
            $( document ).ready(function() {
                $("#sendButton").click(function(){
                    var val = $('#textbox').val();
                    $('<li class = "list-group-item">'+val+'</li>').appendTo('#ulList');
                });
            });
        }
    </script>

This is in my body tag

<input type="text" id="textbox"></input>
<button type="button" class="btn btn-lg btn-success" id="sendButton" onclick=addNames()>add</button>
<ul class="list-group" id="ulList"></ul>

The problem i am facing is that i have to click the add button twice to get any output on the screen(ONLY THE FIRST TIME, then the script works properly on one click) but the output is not a desired output.

this is my output

i get multiple entries of the text in the textbox, even on a single click. please help.
-thanx

6 Answers6

1

Alternative way (modify your function like this):

function addNames(){
    var val = $('#textbox').val();
    $('<li class = "list-group-item">'+val+'</li>').appendTo('#ulList');
}

onclick=addNames() attribute will just call that function

Gene R
  • 3,684
  • 2
  • 17
  • 27
0

Remove the addNames function it is wrapped in. Also, remove the reference to the addNames function in the html.

You should also be aware, for many reasons outlined here, that you should avoid using onclick in your html.

<input type="text" id="textbox"></input>
<button type="button" class="btn btn-lg btn-success" id="sendButton">add</button>
<ul class="list-group" id="ulList"></ul>

  $( document ).ready(function() {
    $("#sendButton").click(function(){
     var val = $('#textbox').val();
     $('<li class = "list-group-item">'+val+'</li>').appendTo('#ulList');
     });
  });
Community
  • 1
  • 1
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
0

You have used both jquery and javascript in your code. It can be simple done by:

<script type="text/javascript">
            $( document ).ready(function() {
                $("#sendButton").click(function(){
                    var val = $('#textbox').val();
                    $('<li class = "list-group-item">'+val+'</li>').appendTo('#ulList');
                });
            });
    </script> 

<input type="text" id="textbox"></input>
<button type="button" class="btn btn-lg btn-success" id="sendButton">add</button>
<ul class="list-group" id="ulList"></ul>
Priya jain
  • 753
  • 2
  • 5
  • 15
0

Check this out

 <script type="text/javascript">
         $( document ).ready(function() {
                $("#sendButton").click(function(){
                    var val = $('#textbox').val();
                    $('<li class = "list-group-item">'+val+'</li>').appendTo('#ulList');
                });
            });
    </script>
Santosh Ram Kunjir
  • 1,074
  • 1
  • 12
  • 23
0

The Problem: On click on button, you were firing on function, that fires one more click event. That's why multiple entries.

The Solution: Remove that function and use only jQuery.

The Advise: Try to avoid mixing jQuery and JS.

 $( document ).ready(function() {
                $("#sendButton").click(function(){
                    var val = $('#textbox').val();
                    $('<li class = "list-group-item">'+val+'</li>').appendTo('#ulList');
                });
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="textbox">
<button type="button" class="btn btn-lg btn-success" id="sendButton" onclick=addNames()>add</button>
<ul class="list-group" id="ulList"></ul>
Kalpesh Singh
  • 1,649
  • 13
  • 16
0

The issue here is that the first time the user clicks, you are registering an additional callback on click. Instead, you can do all your work on the existing callback you created: addNames.

Here is currently what is happening.

  1. The user clicks on #sendButton
  2. The function addNames is called.
  3. addNames is going to register a click event on #sendButton (This is useless, since the addNames function is already the function that is called onclick! It is also unnecessary to use $(document).ready since by this time the DOM is guaranteed to already be ready.).
  4. The element is not added because that function has not been triggered yet (Remember, all you did in the previous step was register the event).
  5. The next time the user clicks, addNames will be called and so will your registered callback.

Here's the code that would work for you:

function addNames(){
  var val = $('#textbox').val();
  $('<li class = "list-group-item">'+val+'</li>').appendTo('#ulList');
}
richardaday
  • 2,814
  • 1
  • 22
  • 17