Tuesday, 21 January 2014

How to remove previous append value JQuery

Leave a Comment
From my previous post to append dropdownlist value using jQuery .If you follow the step by step. The code actually work. But what will happen if the previous value are not remove and the first dropdown keep changed. The second dropdown value will keep increasing without remove the append  value.

Here is my solution to remove the previous append value to the dropdownlist.

<script type="text/javascript">
        $(document).ready(function () {
            
            var counter1 = 0;
            $("#dropdownCountry").change(function (e) {
                var obj1 = { Country: $("#dropdownCountry").val() };
                var isRemove1 = false;
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "http://localhost:3323/AjaxWebService.asmx/getState",
                    data: JSON.stringify(obj1),
                    dataType: "json",
                    success: function (data1) {
                        var appenddata1 = "";
                        var jsonData1 = JSON.parse(data1.d);
                        if (counter1 == 0)
                            counter1 = jsonData1.length;
                        else {
                            isRemove1 = true;
                        }
                        for (var i = 0; i < jsonData1.length; i++) {
                            appenddata1 += "<option id=\"appendDataTemp1\" value = '" + jsonData1[i].SHORT_NAME + " '>" + jsonData1[i].FULL_NAME + " </option>";
                        }
                        if (isRemove1) {
                            for (var j = 0; j < counter1; j++) {
                                $("#appendDataTemp1").remove();
                            }
                            counter1 = jsonData1.length; isRemove1 = false;
                        }
                        $("#dropdownState").append(appenddata1);
                    }
                });
            });
        });
    </script>

Note : please refer my previous post here to append dropdownlist value

By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.