0

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);
6
  • This question looks incomplete. Did you forget something? Can you add a little more detail? Commented Sep 14, 2009 at 23:40
  • 3
    You do realize jQuery is javascript, right? :) Commented Sep 14, 2009 at 23:45
  • ok. Here's the complete code. Sorry for not doing that earlier. Also the answers posted earlier do not seem to work. Not sure if I understand why thats the case. Sa Commented Sep 15, 2009 at 0:22
  • @sa - I still do not understand. Do you mean 'hide' it if it is empty? Commented Sep 15, 2009 at 0:35
  • Hi Karim, Yes. Actually I have a custom function HideIfEmpty() that I can use. So if these values do not exist. It should just call this function and that will hide it. Commented Sep 15, 2009 at 0:46

3 Answers 3

4
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();
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

try:

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

Comments

0
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
}

Comments

Your Answer

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