How to call javascript in href in sharepoint column

3.4k views Asked by At

I do have a calculated column in sharepoint which creates an anchor tag in which href have a javascript function call to test method but it's not getting called what i am missing here. Code :

<script type="text/javascript">
function test()
{
alert("hiii");
} 
</script>

and calculated column :

<a href='javascript:test()'> Click </a>

Thanks in advance

4

There are 4 answers

0
Phoenix404 On BEST ANSWER

Pure JS:

function test(){
 alert("I am alerted");
} 
<a onclick='test()' > Click </a>

jQuery

$(document).ready(function (){
  $("#myID").on("click", function (){
    alert("I am alerted !");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="myID" > Click </a>

0
Marcel P On

The attribute in the anchor you are looking for is called onclick:

<a onclick="test()"> Click </a>

This should do the trick I guess. Check the w3c-schools site for a more detailed description: W3C Schools on the onclick HTML attribute

0
HubertS On

Please try this way:

<a href='javascript:void(0)' onclick='test()'> Click </a>
<script type="text/javascript">
function test()
{
   alert("hiii");
} 
</script>
0
Nico On

If I understand, you need to change the display (html generated) of a field in SharePoint.

You are un 2013, you can use JSlink property of your field. With this, you can link a file JS to your field, and this file will be executed every time that sharepoint will generate the rendering of your field.

Check this link to understand how it work and see some exemples ;) : https://code.msdn.microsoft.com/office/Client-side-rendering-JS-2ed3538a

hope this will help you