View Full Version : Limit spawned fields

Dec 31st, 2008, 11:46 AM
I have the following JavaScript... I would like to limit the number of fields it spawns to 5, how do i do that ? Thanks in advance:

// When the DOM has loaded, init the form link.
// Get the add new upload link.
var jAddNewUpload = $( "#add-file-upload" );

// Hook up the click event.
.attr( "href", "javascript:void( 2 )" )
function( objEvent ){

// Prevent the default action.
return( false );


// This adds a new file upload to the form.
function AddNewUpload(){
// Get a reference to the upload container.
var jFilesContainer = $( "#files" );

// Get the file upload template.
var jUploadTemplate = $( "#element-templates div.row" );

// Duplicate the upload template. This will give us a copy
// of the templated element, not attached to any DOM.
var jUpload = jUploadTemplate.clone();

// At this point, we have an exact copy. This gives us two
// problems; on one hand, the values are not correct. On
// the other hand, some browsers cannot dynamically rename
// form inputs. To get around the FORM input name issue, we
// have to strip out the inner HTML and dynamically generate
// it with the new values.
var strNewHTML = jUpload.html();

// Now, we have the HTML as a string. Let's replace the
// template values with the correct ones. To do this, we need
// to see how many upload elements we have so far.
var intNewFileCount = (jFilesContainer.find( "div.row" ).length + 1);

// Set the proper ID.
jUpload.attr( "id", ("file" + intNewFileCount) );

// Replace the values.
strNewHTML = strNewHTML
new RegExp( "::FIELD2::", "i" ),
new RegExp( "::FIELD3::", "i" ),
("presoFile" + intNewFileCount)

// Now that we have the new HTML, we can replace the
// HTML of our new upload element.
jUpload.html( strNewHTML );

// At this point, we have a totally intialized file upload
// node. Let's attach it to the DOM.
jFilesContainer.append( jUpload );

Jan 2nd, 2009, 06:49 AM
I have the following JavaScript
No, you have not a pure native javascript code, that looks like a framework code. Even a framework is a javascript application, it uses custom methods and functions whom we can not imagine what they suppose to do, unless we know which framework is that. Now, what is that: JQuery? Prototype? Mootools? Another... which one?

Note: You should have not used frameworks for simple tasks like that.

Jan 2nd, 2009, 06:08 PM
Thanks Kor :)

What I really want is:
I have a form that uploads multiple images, I want the form instead to be able to support upto a max of 5 files. And have the files shown like e.g:
(Where when you add more files they will display underneeth eachother) UPTO a max of 5 files

However I was told that this JavaScript Snippet would do the job... Do you have any other code JavaScript or not that would do the above job. I am not experienced within client-side-coding :(

Thanks in advance and happy new year :)

Jan 3rd, 2009, 08:26 AM
The problem with javascript is that you need to be experienced with client-side coding in order to create, use, debug codes. You can not simply use a javascript snippet without knowing the language. Things do not work like that in web design/developing. A web designer / developer must know javascript, same as he must know HTML and CSS. These 3 languages are the basis and required.

Jan 5th, 2009, 03:28 PM
problem is I know PHP/xHTML and CSS... but these three cannot help me do something client-side that is dynamic!

JavaScript / mootools could...

But of course I can understand the basics when I see the JS code... but I was just asking to tweak the current code to enable a limit... there must be some sort of "count" command like in PHP.. but I cannot figure out how to get that to work in the JS code... :(

Jan 5th, 2009, 05:54 PM
ok. The problem with those frameworks is that you will never know what is hidden behind their custom functions/methods because they are custom. They are not native javascript methods. You may guess or you may start searching for those custom functions/methods in the core of the framework. Which is an intricate job. You could ask people who create that framework. A javascript coder will never be able to help you if he does not know that framework by heart. And usually only the creators are able to.

Jan 8th, 2009, 11:43 AM
hey again Kor ...I really appreciate your words...

Anyways what is used is jQuery


Jan 8th, 2009, 12:20 PM
Ok, I have moved your thread into the Javascript Frameworks Forum, where I think you might get the proper support.