Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Ternary operator on a element attribute

So I don't know if this is possible, but I wanted to check for a window.OneTrust object and if it doesn't exist, to keep the attribute as src, otherwise keep it as data-src and change the type.

Here is what I'm talking about:

If window.OneTrust exists, keep it as:

<script
  type='text/plain'
  id='google-maps-api-script'
  className='optanon-category-C0004'
  data-src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
></script>;

Otherwise, have it as:

<script
  type='text/javascript'
  id='google-maps-api-script'
  className='optanon-category-C0004'
  src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
></script>;

Is there a way that I can have a ternary operator for type/src instead of doing it like this?

if (!window.OneTrust) {
  <script
    type='text/javascript'
    id='google-maps-api-script'
    className='optanon-category-C0004'
    src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
  ></script>;
} else {
  <script
    type='text/plain'
    id='google-maps-api-script'
    className='optanon-category-C0004'
    data-src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
  ></script>;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use document.write in a <script> element.

<script>
if (!window.OneTrust) {
  document.write(`<script
    type='text/javascript'
    id='google-maps-api-script'
    class='optanon-category-C0004'
    src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
  ><\/script>`);
} else {
  document.write(`<script
    type='text/plain'
    id='google-maps-api-script'
    class='optanon-category-C0004'
    data-src="https://maps.googleapis.com/maps/api/js?key=<?php echo get_field('google_maps_api_key', 'option') ?>&v=weekly"
  ><\/script>`);
}
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!