Check if an ID exists or not using jQuery

Viewed 59114

Possible Duplicate:
Finding whether the element exists in whole html page

Is there any way to check for if a ID $('#ID') exists in jQuery

Ex:

$('#cart').append('<li id="456">My Product</li>');

After running append() to something like this I want to check if my ID $('#456') exists or not. If it exits I want to change the text, else I want to append a new

  • .

    $(document).ready(function() {
            $('#RAM,#HDD').change(function() {
    
                var product = $(this).find("option:selected").attr("product");
    
                $.ajax({
                    url: 'ajax.php',
                    type: 'post',
                    data: $(this).serialize(),
                    dataType: 'json',
                    success: function(data) {
                        $('#cart').empty();
                        $.each(data, function(index, value) {
                            $('#cart').append('<li id="'+product+'">'+ value['price'] +'</li>');
                        });
                    }
                });
            });
        });
    
  • 4 Answers
    Related