0

How do I call a JavaScript function from href tag in html? I've created a few tabs and when a user clicks on one of them the href along with JavaScript function should get called.

<?php if($order == 1){ ?>
<li class="cat-one">
    <a href="javascript:void(0)" onclick="loadProducts(<?php echo $categoryId ?>)"> 
        <?php echo $categoryName ?> 
    </a>
</li>
<?php } ?>

This is my JavaScript:

function loadProducts($categoryId)
{
    alert("Hello World!");
    return false;
}

Why doesn't the alert trigger in my function? I am running 2 jQueries on the same page.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ScoRpion
  • 11,364
  • 24
  • 66
  • 89
  • Visit the page, view source and look for errors in the part you have mentioned. Is categoryId an integer? You didnt surround it with quotes so unless it is, javascript will see something like `loadProducts(IAmACategoryId);` which will fail. What javascript error do you get when you click the link? – James Nov 16 '11 at 14:21

6 Answers6

2

What if you try this:

<a href="#" onclick="loadProducts(<?php echo $categoryId ?>); return false;"> 
Niels
  • 48,601
  • 4
  • 62
  • 81
0

Try:

<script type="javascript" >
function loadProducts(categoryId)
{
alert("Hello World!" + categoryId);
return false;
}

</script>

And also:

<a href="#" onclick="loadProducts(<?php echo $categoryId ?>); return false;" > 
  <?php echo $categoryName ?> 
</a>
ysrb
  • 6,693
  • 2
  • 29
  • 30
  • Why can't he do `function loadProducts($categoryId) {`? Or am I missing something? – PeeHaa Nov 16 '11 at 12:05
  • @PeeHaa: While variables *can* begin with a dollar sign in JavaScript, it is not mandatory and not often used. [Here is a question about when to use a dollar sign in a variable name in JavaScript](http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign). – RickN Nov 16 '11 at 12:27
0

I just checked the JavaScript, you make a mistake while declaring script tag, the correct code is below

<script type="text/javascript" >
function loadProducts($categoryId)
{
alert("Hello World!");
return false;
}
</script> 
0

From your script above I really don't see nothing wrong with the JavaScript. Try debugging your PHP first to see if the expected value like ($categoryId) is exactly what you expect to be.

Your are missing something also from the script tag

<script type="text/javascript">
// Your script goes here... 
</script>
Robert Wilson
  • 659
  • 1
  • 12
  • 28
0

May be your php script has some errors, Try this example also.

<html>
<body>
<script type= "text/javascript">
function loadProducts(catid){
    alert(""Hello World!"+catid);
    return false;
}
</script>


<?php if($order == 1){ ?>
<li class="cat-one">
    <a href="javascript:void(0)" onclick="loadProducts(<?php echo $categoryId ?>)"><?php echo $categoryName ?> </a>
</li> 

</body>
</html>
Anish
  • 4,262
  • 6
  • 36
  • 58
0

From what I can tell without actually running this code, you can fix a few semi-colons on lines 3 and 4, change the href, and put the parameter for loadProducts in quotes. Try this:

<?php if($order == 1){ ?>
<li class="cat-one">
    <a href="#" onclick="loadProducts('<?php echo $categoryId; ?>')"> 
        <?php echo $categoryName; ?> 
    </a>
</li>
<?php } ?>

Let me know if it works out for you.