Regex search matching an element-ID, or element-Name/Value
I want to create a script (JavaScript) to enumerate through all "radio"
elements searching for those that have an "ID" (or perhaps "Name" and
"Value") that matches some regexs. If there is a match, I'll execute
additional code depending on which regex matched. Searching by "ID" should
be easier than searching by "Name" and "Value", but in some cases the
elements don't have an "ID", so in those cases, I'd search for a regex
match by "Name" and "Value".
Here are some typical sets of "radio" elements:
<span>
<input id="ABC123" onclick="somefunction('DEF');" name="GHI" value="JKL45"
type="radio">
<input id="MNO678" onclick="somefunction('PQR');" name="GHI" value="STU90"
type="radio">
</span>
<span>
<input name="GHI" value="JKL45" type="radio">
<input name="GHI" value="STU90" type="radio">
</span>
I have most of the JavaScript worked out below. This script will be saved
as the "URL" of a web-browser shortcut (bookmark/favorite). This is
generally referred to as a "Bookmarklet" (or "Favlet" for IE).
Here's what I have so far:
javascript:
(function(){
function dorado(rado){
/*
Here I want to test if the value of the "ID" for
the element "rado" matches any 1 of 2-or-3 different
regex's. If there is no "ID" then I'd test "Name" and "Value"
If none of the regex's match, I'll return.
Otherwise, I'll execute additional code depending on which regex matched.
*/
}
var x,k,f,j;
x=document.forms;
for (k=0; k<x.length; ++k) {
f=x[k];
for (j=0;j<f.length;++j)
// call dorado() only for "radio" elements.
if (f[j].type.toLowerCase() == "radio")
dorado(f[j]);
}
}
)();
I shouldn't have a problem with any of the JavaScript code, except I need
to know how to do the regex match tests.
No comments:
Post a Comment