As already mentioned, (currently) the rating rating does not take into account the possibility of embedding input inside the label.
Since no one has sent a jquery-free answer, here is mine:
var labels = form.getElementsByTagName ('label'); var input_label = {}; for (var i = 0 ; i != labels.length ; i++) { var label = labels[i]; var input = label.htmlFor ? document.getElementById(label.htmlFor) : label.getElementsByTagName('input')[0]; input_label[input.outerHTML] = (label.innerText || label.textContent);
In this example, for simplicity, the lookup table is directly indexed by input HTML elements. It is hardly effective, and you can adapt it as you like.
You can use the form as a basic element or the entire document if you want to get labels for several forms at once.
Valid HTML is not checked (multiple or missing inputs inside labels, missing input with the corresponding htmlFor identifier, etc.), but feel free to add them.
You might want to trim label texts, as in this case input elements are always placed in trailing spaces.
kuroi neko Dec 17 '13 at 11:23 2013-12-17 11:23
source share