1

I want to delete a textbox. If I use remove() it is just hiding.If i inspect, it is displaying "display:none" as attribute.

Basically my program is fetching a number from the numberbox n creating those many textboxes. and I am displaying the values of all text boxes. After entering data into all the textboxes, if I delete one of them n print, i dont want the data of the deleted text box to print. But my code is printing that. Is there any way to remove the textbox completely? remove() is not working as i expected here.

Here is my code

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        var i=1;
        var id=1,text;
//      text = '<li><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';

        $("#ok").click(function(){
            var number = document.getElementById("num").value;
            //alert(number);
            for(var $j=i; $j<= number; $j++){
                text = '<li><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';
                $("ol").append(text);
                id++;i++;
            }

        });

        $("#btn1").click(function(){
            text = '<li ><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';
            $("ol").append(text);
            id++;i++;
        });

        $('.divi').on('click','.btn2',function(){
          $(this).parents("li").slideUp('medium',function(){
                $(this).parents("li").remove();
                i--;
            });     
        }); 

        $("#btnprint").click(function(){
            var number = document.getElementById("num").value;
            var msg="";
            for(var j=1; j<=number; j++){
                msg+=document.getElementById("input_" +j).value + "<br/>";
            }
            document.getElementById("printdiv").innerHTML=msg;
        });

    });
</script>
</head>

<body>
    <input type="number" id="num" autofocus="true"></input>
    <button id="ok">OK</button>
    <br/><br/>
    <button id="btn1">+</button>

    <div class="divi">
        <ol>
        </ol>
    </div>

    <button id="btnprint" >Print Data</button>
    <div id="printdiv"></div>

</body>

</html>
2
  • Use $(this).remove(); cause you're already in the scope of your LI element. Commented Apr 7, 2016 at 5:16
  • ya I know tat. But here remove() is not completely removing the element. Instead it is removing the element from the view which is same as hiding. I meant the data is still present even after removing. Commented Apr 7, 2016 at 5:18

4 Answers 4

2

The issue is in this line:

$(this).parents("li").remove();

The selector is wrong. $(this) already refers to "li" and on top of that you are selecting li parents of li which is wrong, hence remove() doesn't work. BTW, display:none is due to the slideUp() function.

Make that line:

$(this).remove();

A working demo: http://jsfiddle.net/GCu2D/1186/

Sign up to request clarification or add additional context in comments.

7 Comments

$(this).remove(); doesn't work. It just remove the button.
@MdMahfuzurRahman $(this) refers to li inside the slideUp. Scope of $(this) changes inside the function.
@K K "demo" isn't printing.
@Poojanagarajrao Demo is just to show you the delete part, I believe this is where you were stuck
@K K deleting is ok.. but I dont want the data of the deleted element to be printed
|
1

try:

$(document).ready(function() {
  $(document).ready(function() {
    var i = 1;
    var id = 1,
      text;
    //      text = '<li><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';

    $("#ok").click(function() {
      var number = document.getElementById("num").value;
      //alert(number);
      for (var $j = i; $j <= number; $j++) {
        text = '<li><input id="input_' + id + '" type="text"/> <button class="btn2">-</button> </li>';
        $("ol").append(text);
        id++;
        i++;
      }

    });

    $("#btn1").click(function() {
      text = '<li ><input id="input_' + id + '" type="text"/> <button class="btn2">-</button> </li>';
      $("ol").append(text);
      id++;
      i++;
    });

    $('.divi').on('click', '.btn2', function() {
      $(this).parent().slideUp('medium',function(){
                $(this).remove();
                i--;
            });
    });

     $("#btnprint").click(function() {
  var number = $("#num").val();
  var msg = "";
  for (var j = 1; j <= number; j++) {
    msg += $('ol li').eq(j-1).find('input[id^="input_"]').val() + "<br/>";
  }
  $("#printdiv").html(msg);
});

  });

});

http://jsfiddle.net/GCu2D/1187/

or

  $("#btnprint").click(function() {
      var number = $("#num").val();
      var msg = "";
      $('input[id^="input_"]').each(function(){
        msg += $(this).val() + "<br/>";
      });
      $("#printdiv").html(msg);
    });

http://jsfiddle.net/sxjL4t62/

5 Comments

Data isn't getting printed
its not displaying any data. And the error is "addrow.php:43 Uncaught TypeError: Cannot read property 'value' of null"
or jsfiddle.net/sxjL4t62 if you don't use the counter number in the first input field
thank you @madalin ivascu its almost done.. but the data undefined is displaying. I'll try solving that. Major part is done. Thanq
jsfiddle.net/sxjL4t62 is working completely fine thanq
0

Use this: See Demo

    <html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        var i=1;
        var id=1,text;
//      text = '<li><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';

        $("#ok").click(function(){
            var number = document.getElementById("num").value;
            //alert(number);
            for(var $j=i; $j<= number; $j++){
                text = '<li><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';
                $("ol").append(text);
                id++;i++;
            }

        });

        $("#btn1").click(function(){
            text = '<li ><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';
            $("ol").append(text);
            id++;i++;
        });

        $('.divi').on('click', '.btn2', function() {
             $(this).parents("li").slideUp('medium', function() {
             $(this).remove();
             i--;
          });
       });

         $("#btnprint").click(function(){ 
            var number = $(".divi ol li").length;

            var msg="";
            for(var j=1; j<=number; j++){
                msg += document.getElementById("input_" +j).value + "<br/>";
            }
            document.getElementById("printdiv").innerHTML=msg;
        });

    });
</script>
</head>

<body>
    <input type="number" id="num" autofocus="true"></input>
    <button id="ok">OK</button>
    <br/><br/>
    <button id="btn1">+</button>

    <div class="divi">
        <ol>
        </ol>
    </div>

    <button id="btnprint" >Print Data</button>
    <div id="printdiv"></div>

</body>

</html>

3 Comments

Data isn't getting printed
Which data is not getting printed ?
no data is printed.. I want the data of the visible textboxes to be printed.
0

Hello Dear now your code is working as per as your requirement. Please only replace your code with this code. Written Here....

<!DOCTYPE html>
<html>
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>         
        <script>
            $(document).ready(function() {
                var i = 1;
                var id = 1, text;
                //      text = '<li><input id="input_'+id+'" type="text"/>      <button class="btn2">-</button> </li>';

                $("#ok").click(function() {
                    var number = document.getElementById("num").value;
                    //alert(number);
                    for (var $j = i; $j <= number; $j++) {
                        text = '<li><input id="input_' + id + '" type="text"/> <button class="btn2">-</button> </li>';
                        $("ol").append(text);
                        id++;
                        i++;
                    }

                });

                $("#btn1").click(function() {
                    text = '<li ><input id="input_' + id + '" type="text"/> <button class="btn2">-</button> </li>';
                    $("ol").append(text);
                    id++;
                    i++;
                });

                $('.divi').on('click', '.btn2', function() {
                    var tempIndx = $(this).parent().index();
                    $(this).parents("li").slideUp('medium', function() {
                        $('.divi ol').find("li").eq(tempIndx).remove();
                        i--;
                    });
                });

                $("#btnprint").click(function() {
                    var number = document.getElementById("num").value;
                    var msg = "";
                    for (var j = 1; j <= number; j++) {
                        msg += document.getElementById("input_" + j).value + "<br/>";
                    }
                    document.getElementById("printdiv").innerHTML = msg;
                });

            });
        </script>
    </head>

    <body>
        <input type="number" id="num" autofocus="true">
        </input>
        <button id="ok">
            OK
        </button>
        <br/>
        <br/>
        <button id="btn1">
            +
        </button>

        <div class="divi">
            <ol></ol>
        </div>

        <button id="btnprint" >
            Print Data
        </button>
        <div id="printdiv"></div>

    </body>

</html>

2 Comments

its not displaying any data. And the error is "addrow.php:43 Uncaught TypeError: Cannot read property 'value' of null"
Hi Pooja right now copy and paste your code on my editor then i can do it how can this possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.