Javascript write to item


Userlevel 6
Badge +13

Hi,

Odd issue I'm facing.

I have a form that has a RTF control called Board Update. In this control a user will write text.

I then have a second RTF called Commentary. 

Lastly I have a button that executes a function to write the contents of Board Update to the column connected to Commentary, prepending it to the existing contents of the Commentary column (not overwriting), clears the value from Board Update and then refreshes the form to show the change. There is also some other text written from another control to the Commentary field to show the User and current time and date. 

 

Typical scenario is that a user will type some text into Board Update and save the form.

They will then come back to the form at a later date, click the button to commit the contents of Board Update to Commentary and blank out the Board Update column.

 

(Before click)

18656iD46C4C1A9586B7F3.png

(after click)

18657i7927A531AD7CAB86.png

 

So the problem is this.

When text is entered into the Board Update control and then the button is clicked straight away, it works fine.

When text is entered into the Board Update control and then the form is saved, the user returns to the form, clicks on the Board Update control to bring focus to it, and then clicks on the button, it also works.

However, when text is entered into the Board Update control, the form is saved, then user returns and clicks on the button without bringing the focus to Board Update control first, it fails to put a carriage return after the user and date. Like this

 

18658iB31C3656AA19187A.png

 

I've tried to counter this by putting a line in the script to add "focus" to the control before executing the script, but this hasn't worked.

 

My script is as follows:

function SetBoardUpdate() { NWF$("#" + varBoardUpdate).focus(); var BoardUpdate = NWF$("#" + varBoardUpdate).parent().siblings('input').val(); var oldBody= NWF$("#" + varBody).val(); var CurrentUser = NWF$("#" + varCurrentUser).val(); var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl); var oList = clientContext.get_web().get_lists().getByTitle('Tasks'); JSRequest.EnsureSetup(); var itemId = JSRequest.QueryString['ID']; this.oListItem = oList.getItemById(itemId); oListItem.set_item('Body', CurrentUser + '  ' + BoardUpdate + '  ' + oldBody); oListItem.set_item('BoardUpdate', ''); oListItem.update(); clientContext.load(oListItem); clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); } function onQuerySucceeded() { window.location.reload(); }

 

Does anyone have any ideas as to how to fix this "lack of focus" issue.


Thanks

 


2 replies

Userlevel 5
Badge +14

Are you certain that the string with the phantom carriage return is the BoardUpdate? Could it not be from the CurrentUser var, which seems to be getting the Date appended to it at some point?

Additionally, could you not just regex the different parts of the string to see if they either had a "/r" or not and add it if it didn't? Just looking at your code though, nothing about it indicates that there should even be a return or newline (/r & /n respectively) as you're using non-breaking spaces in between your strings. 


Userlevel 6
Badge +13

Well, I've changed the control from RTF to plain text and the issue is resolved, so I'm sure it's the BoardUpdate RTF control.


 


I don't know whether plain text will be an acceptable solution for us. But it highlights where the issue lies I think.

Reply