13

How to call a JavaScript Function from href tag in html? I have created few tabs. I want when user clicks on any tab the href along with JavaScript function should get called. How can I do it?

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

This is my JavaScript:

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

</script>
3
  • Do you have some code to show us? Commented Nov 16, 2011 at 9:56
  • You need to share some code if you want any help. If you want to display a tab and execute a JavaScript function when the user click on that tab you would generally solve this without server-side code, i.e. without PHP. Commented Nov 16, 2011 at 9:57
  • You have to make a difference between server and client side actions. PHP is only alvailable on server. HTML, CSS and Javascript is only available on client side. The a tag, with attribute 'href' is only a HTML -also client side only- stuff. Commented Nov 16, 2011 at 9:58

5 Answers 5

22

In the href tag, without specifying a link, specify your javascript function name.

For example

<a href='javascript:myFunction()'> Click Me! <a/>
Sign up to request clarification or add additional context in comments.

1 Comment

in ie9 this causes the function to run and navigation away from the page. Never use to do that in ie7. workarounds?
8

Don't use the href, use the onclick attribute for this

<a href="http://www.example.com" onclick="return confirm('are you sure?')">test</a>

see this example

EDIT(since code added in question): this should call your JS function:

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

1 Comment

And How Can I Write A Function Withen My <Script> Tags
0

Try this

  <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

This should solve your purpose:

<a href="javascript:void(0);" onclick="yourFunction();">Link to Click</a> 

Comments

0

If you have jquery library you can call with class:

$( ".loadProducts" ).click(function( event ) {
      // Default action of the event will not be triggered.
      event.preventDefault();
      // Get category ID
      var categoryId = $(this).attr('data-categoryid');
      // TODO
      alert("Hello World!");
      return false;
});

For:

<li class="cat-one"><a href="#" class="loadProducts" data-categoryid="<?php echo $categoryId;?>" > <?php echo $categoryName ?> </a></li>

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.