I have a link in HTML/PHP, and on the click event, I want JavaScript/Jquery code activated/executed:
PHP/HTML:
echo "<a id='kodelinka' class='linknew' href=''>$kd_mrp</a>";
JavaScript:
<script type="text/javascript">
$('#kodelinka').click(function(){
var value = "bbbb";
alert("aaaaa");
$('#kodelink').fadeOut();
});
</script>
How do I activate the script?
Try like
echo "<a id='kodelinka' class='linknew' href='javascript:;'>$kd_mrp</a>";
You need to prevent the anchor tag default action.Also you can do like
$('#kodelinka').click(function(e){
e.preventDefault();
Makesure your script is working in your page
try to make href to go js
echo "<a id='kodelinka' class='linknew' href='javascript:void(0);'>$kd_mrp</a>";
For the PHP part, you can use either
<?php echo "<a id='kodelinka' class='linknew' href=''>$kd_mrp</a>"; ?>
or if you want to have the in the html
<a id='kodelinka' class='linknew' href=''><?php echo $kd_mrp; ?></a>
Now for the javascript part
<script type="text/javascript">
$('#kodelinka').click(function(){
var value = "bbbb";
alert("aaaaa");
$('#kodelink').fadeOut();
});
</script>
Incase if you didn't know, you need to have jquery for the alert function
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
here is 3 samples:
<a href="####"></a>
<a href="javascript:void(0)"></a>
<a href="javascript:void(null)"></a>
don't use href=''