PDA

View Full Version : Targeting ID



phasejump
09-04-2010, 12:23 AM
<script language="JavaScript"><!--
function insertNthChar(string,chr,nth) {
var string = document.getElementById('mycoupon').innerHTML;
var output = '';
for (var i=0; i<string.length; i++) {
if (i>0 && i%4 == 0)
output += chr;
output += string.charAt(i);
}
return output;
}
//--></script>



<body onLoad="alert(insertNthChar('', '-', 4));">

The current code works perfectly with a pop up alert, but I want it to target an ID instead. The ID is 'mycoupon'. Does anyone know how to do this?

jscheuer1
09-04-2010, 06:03 AM
I'm not sure what you mean. Try this:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function insertNthChar(string, chr, nth){
var sl = string.length, i = 0, output = '';
for (i; i < sl; ++i){
if (i && !(i % nth)){
output += chr;
}
output += string.charAt(i);
}
return output;
}
</script>
</head>
<body onload="var d = document.getElementById('mycoupon'); d.innerHTML = insertNthChar(d.innerHTML, '-', 4);">
<div id="mycoupon">
The current code works perfectly with a pop up alert, but I want it to target an ID instead. The ID is 'mycoupon'. Does anyone know how to do this?
</div>
</body>
</html>

If that's not what you're looking for, please be more specific.

phasejump
09-04-2010, 06:37 AM
John, that is EXACTLY what I was looking for. Thank you!