0

I am using Ajax where the server queries a database using SELECT someColumn FROM someTable, returns someColumn to the client, and the client updates an element using $('#someElement').text(someColumn);

Works great unless someColumn returns null, in which the page displays "null" instead of my desired empty string. Originally, I was dealing with this at the database level using SELECT COALESCE(someColumn,''), however, am considering changing to the client level using $('someElement').text(someColumn==null?'':someColumn); I am sure there are other solutions as well. What is the best practice method to display null as nothing?

3
  • 1
    Am concerned this question may be "not constructive" insofar as it is "likely solicit debate, arguments, polling, or extended discussion", but for what it's worth I'd retain the information that the value is NULL for as long as possible - i.e. handle that case in the client. Commented Apr 29, 2013 at 10:07
  • @eggyal. I was concerned about asking this question as well, but finally decided there probably is a best practice and it will not result in endless debate. I probably will go with your recommended approach of dealing with it at the client. Commented Apr 29, 2013 at 10:11
  • Use json_encode() on the data first, server-side. It will generate 'text', but also null which will achieve the desired result. Commented Jun 4, 2013 at 0:32

2 Answers 2

0

You could try

$('#someElement').text(someColumn || '')

Edit

Will not work if someColumn is a numerical value and has a value that should be displayed, but evaluates to false. But it should work with string values.

2
  • Will this cause problems if someColumn is 0? Commented Apr 29, 2013 at 10:08
  • If it is the numerical value 0, then yes. If it is a string, then no. Commented Apr 29, 2013 at 10:10
0

Maybe you should try;

$('someElement').text(someColumn===null?'':someColumn);

Just to check the two are the same type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.