Chamod Wijesena
Chamod Wijesena

Reputation: 168

How to show empty string when value is NULL in jQuery on .append() funtion

I need to set html value using .append() funtion, so i need to set ajex data values on it. if data set contain null value then showing as 'null' in UI. i need remove that 'null' With blank.

But can't use separate if funtions before the .append(). because have to show many separate values on it, S i Want remove null value on when showing 'null'.

How can i remove this 'null' when showing the value?

$('#diagnosis').append('        <div class="row">\n' +
            '                        <div class="col-sm-1" >\n' +
            '                        </div>\n' +
            '                        <div class="col-sm-2"><label>Mobile</label>\n' +
            '                        <div class="col-sm-9"> ' + data.person.tp+ ' \n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                    <div class="row">\n' +
            '                        <div class="col-sm-1">\n' +
            '                        </div>\n' +
            '                        <div class="col-sm-2"><label>Address</label></div>\n' +
            '                        <div class="col-sm-9"> ' + data.person.address + ' \n' +
            '                        </div>\n' +
            '                    </div>');

enter image description here

Upvotes: 1

Views: 1177

Answers (1)

Nick
Nick

Reputation: 147196

Since null is "falsey", you can use an or to replace a null value with a default string, for example:

'<div class="col-sm-9"> ' + (data.person.tp || '') + ' \n' +

Upvotes: 2

Related Questions