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.

1
  • 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? Commented Nov 16, 2011 at 14:21

6 Answers 6

2

What if you try this:

<a href="#" onclick="loadProducts(<?php echo $categoryId ?>); return false;"> 
Sign up to request clarification or add additional context in comments.

Comments

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>

2 Comments

Why can't he do function loadProducts($categoryId) {? Or am I missing something?
@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.
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> 

Comments

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>

Comments

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>

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.