2

The Issue

I have struggled create a calculated column in my SharePoint list that renders a button with an onclick event to start a javascript function while passing two column values from the list item.

After searching the internet high and low for answers, I am convinced that this may not be possible with calculated columns. Is this even possible?

The SharePoint list

The user will select the "Active" button from the list item row and the Javascript function should start

My SharePoint list

The Javascript code to be started from the "Active" button

The button should create the following code for each list item:

<input type='button' value='Active' onclick='examplefunction(Title,Status)'/>")

For the first list item in the row this would be:

<input type='button' value='Active' onclick='examplefunction("Title 1","Active")'/>")

To correctly render the html, the following calculated column formula is used (formatted as a number):

=CONCATENATE("<input type='button' value='Active' onclick=","examplefunction('",Title,"','",Status,"')"," />")

The Javascript for the function (via a Content Editor webpart)

<script type="text/javascript">
function examplefunction(Title,Status) {
alert(Title);
alert(Status);
}
</script>

My results

I have tried countless times to write and rewrite the calculated column formula (with and without using CONCATENATE) but I cannot pass the title or status column values.

I usually get the following error in console:

Uncaught SyntaxError: Invalid or unexpected token

If this is possible, how would I write the formula? in the limited examples available online, I have not been able to replicate that success.

2

I have a solution! For those with the same problem please checkout:

http://www.viewmaster365.com/#/How

Danny from Viewmaster has a great page on HTML Calculated Columns and Javascript! I was able modify his example formula code to solve my problem:

="<input type='button' value='Active' onclick=""examplefunction( '"
 & Title
 & "', '"
 & Status
 & "'); "">"

Thanks Danny.

  • Yes, the need-to-know is those double-double-quotes "" so the Formula will output a single-double-quote " ; you can then use single quotes in the JavaScript part. Be sure to read all the Pros and Cons on that page: viewmaster365.com/#/How – Danny '365CSI' Engelman Jan 11 '17 at 9:02
  • Note that with Microsoft Modern Experiences they have disabled HTML/JS injecting using this Calculated Column trick. So this all only works on 2007...till 2016/SPonline in CLASSIC mode – Danny '365CSI' Engelman Jan 11 '17 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.