1

I am trying to add a click event to a link in SharePoint. The current href set by SharePoint is href="javascript:ClickOnce()". If I add the following code:

var saveNclose = document.getElementById("diidIOSaveItem");
saveNclose.href = "javascript:JSEPreSaveAction()";
alert(saveNclose.href);

The alert outputs the href I am trying to get, which is javascript:PreSaveAction() but when I look at it in the page it is still ClickOnce().

Why is the alert telling me the href is one thing and the source code and behavior says is another? Is there something causing SharePoints script to take priority over mine?

Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49
  • How are you viewing the source code? – j08691 May 02 '12 at 16:50
  • In Internet Explorer by pressing F12 – Jonathan Eckman May 02 '12 at 16:50
  • Strange, I'm able to do almost exactly what you're doing successfully here: http://jsfiddle.net/xZ6nC/. Could there be a typo: `diidIOSaveItem` should be `didIOSaveItem`? Although if that was the case and `diidIOSaveItem` didn't exist, you'd just get a JS error telling you that `null` has no property `href`... – Drew Gaynor May 02 '12 at 17:08
  • One thing I just thought of, SharePoint is running its own script to add ClickOnce() to the link I believe. I think mine is running, triggering the alert and then SharePoint is changing it back to ClickOnce(). Looking into how I can make sure mine runs last now. – Jonathan Eckman May 02 '12 at 17:12

2 Answers2

0
document.getElementById("diidIOSaveItem").setAttribute("href", "javascript:JSEPreSaveAction()");

alert( document.getElementById("diidIOSaveItem").getAttribute("href") );
Nadh
  • 6,987
  • 2
  • 21
  • 21
0

I was able to do this by adding a timer to add PreSaveAction() to the button 10 seconds after the page had finished loading.

Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49