How do I call my PHP initialized variable inside the string of onclick=""
? I have Initialized $counter=7
and want to use that counter inside onclick=""
function. This is how I did which is not working.
<a class="blogpost page-transition" href="the_permalink();" onclick="gotoPageWithTrans6($counter)">
I also Tried doing this, but the following code also didn't worked.
<a class="blogpost page-transition" href="the_permalink();" onclick="gotoPageWithTrans6(?php $counter ?> )">
Any solution ?
You are only missing the echo statement:
a class="blogpost page-transition" href="<?php echo the_permalink(); ?>" onclick="gotoPageWithTrans6(<?php echo $counter ?> )
You have a typo at <?PHP
start tag.it should be
onclick="gotoPageWithTrans6(<?php echo $counter; ?> )"
or
//php short tags enabled
onclick="gotoPageWithTrans6(<?= $counter ?> )"
instead of
onclick="gotoPageWithTrans6(?php $counter ?> )"
Quick and easy?
onClick="gotoPageWithTrans6(<?= $counter ?>)"
The short PHP
tag allow you to easily output the values of PHP variables right inside your HTML code.
Cleaner short_open_tag
version with correct onclick attribute:
<a class="blogpost page-transition" href="<?=the_permalink()?>" onclick="gotoPageWithTrans6(<?=$counter?>)">