sa
sa

Reputation:

Jquery if clause

I need help to write a if statement using jquery. So if results.d.ProductName is empty do not show `$(prdHtml).html(html);

$.ajax({
    type: "POST",
    url: "Services.asmx/GetProduct",
    data: '{ "fieldName": "' + id + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
success: function(results) {
        var html = '<h3>' + results.d.ProductName + '<h3>'
                    + '<a href=""' + results.d.Url + '</a>';
        $(prdHtml).html(html);

Upvotes: 0

Views: 698

Answers (3)

Half-Dead
Half-Dead

Reputation: 31

success: function(results) {
  // should probably test if d exists before testing for productname
  if (typeof(results.d) == "undefined" || typeof(results.d.ProductName ) != "string")
   return;

  ...code
}

Upvotes: 0

karim79
karim79

Reputation: 342775

success: function(results) {
        if(results.d.ProductName.length) {
            var html = '<h3>' + results.d.ProductName + '<h3>'
                        + '<a href=""' + results.d.Url + '</a>';

                $(prdHtml).html(html);
        } else {
            $(prdHtml).hide();
        }
}

Upvotes: 4

tonycore
tonycore

Reputation:

try:

success: function(results) {
if (results.d.ProductName!="") {
        var html = '<h3>' + results.d.ProductName + '<h3>'
                    + '<a href=""' + results.d.Url + '</a>';
        $(prdHtml).html(html);
}
}

Upvotes: 1

Related Questions