Software Archive
Read-only legacy content
17061 Discussions

call javascript funciton in anchor click

fabrizio_d_
Beginner
627 Views

i added this function into app.js

function showID(productID)
{

alert(productID);

}

how to call from anchor?

this is not working:

<a href="javascript:showID(1);">open product 1</a>

0 Kudos
2 Replies
Chris_P_Intel
Employee
627 Views

Are you writing this from scratch or using the Design tool? If you are using the Design tool, then the Interactivity pane will help you set up scripts on elements.

Many developers prefer to keep the content and any interactive logic separate.  So instead do this:

<!DOCTYPE html>
<html>
<head>
<!-- let's use jQuery. Makes this a little easier -->
<script src="jquery.js"></script>  
<script>
   function showID(productID)
   {
    console.log("product id:", productID); // I prefer console.log to alert. 
    alert(productID);
   }
   $(document).ready(function()
    {
      $("#my-link").click(function(){ showID(1); return false; })
    })
</script>
</head>
<body>
  <a href="#" id="my-link">Click Me</a>
</body>
</html>

 

If you haven't used jQuery, you can read more here: https://learn.jquery.com/about-jquery/how-jquery-works/ 

I don't necessarily recommend jQuery for all projects, but in this case I think it'll help make things a lot easier. Especially if you are newer to web development.

Chris

 

 

0 Kudos
fabrizio_d_
Beginner
627 Views

in the page are many links, each link is generated from a webservice e represents a product. Clicking on it the app navigates into detail page so need to pass the ProductId to showID function. How to pass the product id using jquery ?

Anyway this code is working fine:

<a href="javascript:void(0);" onclick="showID(1);">some product</a>

<a href="javascript:void(0);" onclick="showID(2);">another product</a>

 

0 Kudos
Reply