php - How to toggle elements with javascript

304

Solution:

I would write them both to DOM and usetoggleClass() from jQuery

<?php
$incl = $product->get_price_including_tax();?>
<div >
  <p id="excl" ><?php echo $product->get_price_excluding_tax();?> excl.</p>
  <p id="incl" ><?php echo $incl;?> excl.</p>
</div>
<button type="button">Click Me!</button>

With following CSS:

.mycont > .price.incl {display: none}
.mycont.show_incl > .price.incl {display: block}
.mycont.show_incl > .price.excl {display: none}

And jQuery:

$('button').on('click', function(){
    $('.mycont').toggleClass('show_incl');
});

Fiddle Here

If you want to do it in pure javascript, I would suggest to call a functiononClick event on button, and toggle class like stated here

972

Answer

Solution:

Useternary operator,condition ? expr1 : expr2

The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement[Reference]

Note: Do not mix-up your logic in markup. You should have a separate function for that.

<p id="excl" class="price">100</p>
<button type="button" onclick="document.getElementById('excl').innerHTML=document.getElementById('excl').innerHTML=='500'?'100':'500'">Click Me!</button>
Write your answer




Share solution ↓

Additional Information:

Link To Source
People are also looking for solutions of the problem: git was not found in your path, skipping source download

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

392 php - How to toggle elements with javascript
108 Wordpress is using single.php and page.php instead of index.php for home page
305 php - phpexcel how get cell format
928 javascript - My PHP code doesn't receive the JSON data
727 Combinging a PHP script with another PHP file, jQuery and HTML
607 php - Dynamic buttons href tests with phpunit
864 php - Get content of elements from the html code in preg_match_all
489 javascript - PHP - Linked list "dynamic"
577 macos - Local PHP failing to load HTTPS urls
686 jquery - Display next or previous month in PHP with Ajax calendar

People are also looking for solutions to the problem: php - Extract commas from variable

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.