Results 1 to 3 of 3

Thread: Targeting ID

  1. #1
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Targeting ID

    Code:
    <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>

    Code:
    <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?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure what you mean. Try this:

    Code:
    <!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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    phasejump (09-04-2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    John, that is EXACTLY what I was looking for. Thank you!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •