What is a good way to ignore the tags/variables in a jQuery HTML method?
For instance, if there was no value for company, it would not be included in the HTML.
$('.modal-body').html(`
<p><span style="font-weight:bold;">Name:</span> ${name}</p>
<p><span style="font-weight:bold;">Company:</span> ${company}</p>
<p><span style="font-weight:bold;">Job:</span> ${job}</p>
<p><span style="font-weight:bold;">Title:</span> ${title}</p>
<p><span style="font-weight:bold;">Phone:</span> ${phone}</p>
<p><span style="font-weight:bold;">Email:</span> ${email}</p>
<p><span style="font-weight:bold;">Event Loc:</span> ${addressResult}</p>
`)
4
Answers
You can use if statement like this:
Or using ternary operators like this:
And also please change ${name} because it’s a global variable.
While I frown on using ternary operators for major control flow, inline conditionals are very useful for situations like this.
Not 100% sure where the customer info is coming from but if its an array of object, you can do something like the following.
If your data is in the form of an array of objects (like a common JSON) and you need to display the data of a single object on a modal, you’ll need to be able to select an object and extract the keys and values of said object (See Figure I)
Figure I
HTML
jQuery/JavaScript
Result
The following example is a live version of the code above with a
<select>
that will allow you to change profiles.Example