0

This should be easy for someone, I just can't seem to get the syntax correct. I have the following code, and I'm sure 70% of this can be represented by a loop: could someone enlighten me please?

function AddNewEmail(){
var jFilesContainer = $( "#emails" );
var jUploadTemplate = $( "#email-templates div.template" );
var jUpload = jUploadTemplate.clone(); 
var strNewHTML = jUpload.html();
var intNewFileCount = (jFilesContainer.find( "div.template" ).length + 1); 
jUpload.attr( "id", ("emailedit[" + intNewFileCount + "]") );
strNewHTML = strNewHTML
 .replace(
 new RegExp( "::FIELD1::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD2::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD3::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD4::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD5::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD6::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD7::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD8::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD9::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD10::", "i" ),
 intNewFileCount
 )
 .replace(
 new RegExp( "::FIELD11::", "i" ),
 intNewFileCount
 ) 
 .replace(
 new RegExp( "::FIELD12::", "i" ),
 intNewFileCount
 ) 
;
jUpload.html( strNewHTML );
jFilesContainer.append( jUpload );
}
asked May 9, 2011 at 9:22
1
  • What doesn't work, what is the problem? Commented May 9, 2011 at 9:25

3 Answers 3

2

If you're using regular expressions, use them:

strNewHTML = strNewHTML.replace(/::FIELD\d{1,2}::/gi, intNewFileCount);
answered May 9, 2011 at 9:27
Sign up to request clarification or add additional context in comments.

Comments

2

I would say

strNewHTML = strNewHTML.replace(/::FIELD\d+::/gi, intNewFileCount);

could be a replacement for your whole strNewHTML logic. Not a loop, but shorter anyway.

answered May 9, 2011 at 9:30

Comments

0
answered May 9, 2011 at 9:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.