JS scripts fail because input field ids have changed
-
My pdb_record page has buttons that run JS scripts that access input fields using
document.getElementById(). The ids of the fields used to be of the form “pdb-some_field” but they are now of the form “pdb-some_field-2”, so the scripts no longer work because they can’t find any of the elements.Looking at PDB source code this seems to be because it now appends
$instance_indexto all the ids, which is an “index for tracking multiple instances of a shortcode”. However the page just has one instance of the shortcode:[pdb_record template=bootstrap groups=main,admin,admin_history,profile,address,contact]Looking at the page’s HTML source code there seems to be only one instance of each id, so why do they all have “-2” added?
I could just change all the calls to be of the form
document.getElementById('pdb-some_field-2'), but am reluctant to do so in case the ids change again. I could try usinggetElementsByName('some_field'), and hope that the first one returned is the correct input field, would this always be the case? Also I assume this is less efficient thangetElementById(), which is the normal way of finding an element.What is needed is a reliable way to get the input field element corresponding to the field’s name. Would it be possible for PDB to generate a JS function (e.g.
pdb_getElement('some_field')) that generates the correct id and uses this in callinggetElementById()?What the best long term solution to accessing input fields whose ids might change again in the future?
The topic ‘JS scripts fail because input field ids have changed’ is closed to new replies.