Disable Requester mail Id on Ticket Creation page

Hi guys,
I am trying to disable Requester mail Id field

Below code works but it only works on initial page load, I suppose that has to do with javascript window.Load method.

I tried with below code as well but did not work even when I wrapped that inside $( document ).ready()

Any help would be appreciated .!

1 Like

Hi Prathamesh

try something like this:

const observer = new MutationObserver(function (mutations) {
  console.log('new mutation!')
  const emailField = document.getElementById('helpdesk_ticket_email')
  if (emailField) {
    emailField.disabled = true
    observer.disconnect() // Stop observing once the field is found and disabled
    return
  }
})
window.onload = function () {
  console.log('onload!')

  observer.observe(document.body, { childList: true, subtree: true })
}

2 Likes

Hi Mattia,
Thanks for that.!
I checked on my end, works well for initial render.
However when I tried to navigate back to create ticket page, getting below error.

A not very elegant solution is to change “const” to “var” and not stop the observer.

I hope this fixes it!

var observer = new MutationObserver(function (_) {
  console.log('new mutation!')
  const emailField = document.getElementById('helpdesk_ticket_email')
  if (emailField) {
    emailField.disabled = true
    // observer.disconnect();  // Stop observing once the field is found and disabled
    return
  }
})

window.onload = function () {
  console.log('onload!')
  observer.observe(document.body, { childList: true, subtree: true })
}

It seems code doesn’t get executed after initial render. This would work for static pages as every time we click on something page gets render.
I am not sure if we can use same approach in Single page application. Function doesn’t get call at all for the subsequent renders.

Perhaps you can try embedding the script in a common layout page, such as the header or footer. But I don’t know if this could create any problem if there is a field with the same id in another page.

It is important to comment the line:

observer.disconnect();

Otherwise the observer no longer runs.

Thanks man It works :grinning: :raised_hands:
I guess this Mutation Observer is tracking all the changes from initial page load.
Only thing now I wanted to confirm is, are there any side affects on performance / storage. considering large count of people using support portal day in day out.

1 Like

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.