Get id of an element that triggered the event, utilize id as string
I want to use the triggering elements id, then use it as a string, to perform certain logical operations.
Basically:
<html>
<head>
<script type="text/javascript">
//OK
function getValue(control)
{
alert(control.id);
}
function filterByIDType(ctl) {
var marker = "not set";
var ctlID = ctl.id;
alert(ctlID); //OK
//alert(ctlID.toString()); //Not working, how to use to match particular 'types'
// Not working:
try
{
if(ctlID.Match('Type1'))
{ marker ='Type1';
document.getElementByID(marker+'Lists').value = marker;
}
if(ctlID.Match('Type2'))
marker = 'Type2'
if(ctlID.Match('Type3'))
marker = 'Type3';
}
catch(err)
{
alert('Marker is still:'+marker);
}
}
</script>
</head>
<body>
<h1>My Web Page</h1>
<p id="demo">This is a paragraph.</p>
<a id="idname" onclick="getValue(this)">Click link, get id</a>
<input id="Type1Lists" value="">input type1 here:</input>
<p id="Type1Input" onclick="filterByIDType(this)">Click a paragraph with type1 ip</p>
<p id="Type2Input" onclick="filt开发者_如何学编程erByIDType(this)">click a paragraph with type2 ip</p>
<p id="Type3Input" onclick="filterByIDType(this)">click a paragraph with type</p>
</body>
</html>
I get error:
Object doesn't support this property or method
Try
if (ctlID.match('Type1'))
// ^m, not M
Make
var ctlID = ctl.id;
to
var ctlID = ctl.id.toString();
精彩评论