Jump to content


Regualr expressions and array creation

  • Please log in to reply
1 reply to this topic

#1 ToonMariner

  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 04 September 2006 - 01:33 AM

Hi people,

Bear with me on this is as its a little complicated to explain.

I have a parent div wich contains a variable number of child divs.

Each child div has a series of form elements.

All this depicted here:

<div id="cont">
 <div class="elecont" id="cX">
  <input type="file" name="file[X]" id="file[X]"/>
  <input type="text" name="title[X]" id="title[X] />
  <input type="text" name="alt[X]" id="alt[X]" /.

there can be any number of the child divs with id="cX" in the parent div and X is a numeric variable.

Now you may have guessed that this is a gallery script or similar.

What I have is a link to add and remove these child divs which works fine apart from that when any inputs are given info - particularly the file input box - this value is lost when any other child divs are added/removed.

What I need to do is loop through the elements of the form. If the id of that element contains '[X]' then I need to take the name/value pair and store it into an array. The array should be 2-d with X being the first key and the other part of the element id being the second key.


if i had the follwoing 2 child divs:
<div id="cont">
 <div class="elecont" id="c2">
  <input type="file" name="file[2]" id="file[2]"/>
  <input type="text" name="title[2]" id="title[2] />
  <input type="text" name="alt[2]" id="alt[2]" /.
 <div class="elecont" id="cX">
  <input type="file" name="file[5]" id="file[5]"/>
  <input type="text" name="title[5]" id="title[5] />
  <input type="text" name="alt[5]" id="alt[5]" /.

I would need to construct an array that would be:

Array ( 2=> Array (file=> val, title=> val, alt=> val),
          5=> Array (file=> val, title=> val, alt=> val))

I use 2 & 5 to illustrate that the numbers need not be sequential.

Once I have this array and have added/removed one the child divs I need to loop through the array and set the corresponding values of those elements.

I know I need to use regex to get the correct element name[X] split in to name and X but not sure how to do that nor how to use those in creating the keys for teh array.

If all that made sense and if you can find them maybe you can hire the A-Team....

Sorry, chain of thought went ;)

Any help would be really appreciated....

This is the code I am currently trying yo use....

function grabValues()
 var myArr=[];
 var el = document.forms['galform'].elements;
 var regex = new RegExp(/(\D)\[(\d)\]/);
 for(var i=0;i<el.length;i++)
  if (el[i].name && el[i].name.match(regex))
   switch (el[i].type)
    case 'text':
    case 'file':
     myArr[regex[2]][regex[1]] = el[i].value;
    case 'textarea':
     myArr[$2][$1] = el[i].innerHTML;
   document.getElementById('test').innerHTML += el[i].name+" type = "+el[i].type+"\r\n";
  //el[i].name.match(/\[.\]/g) ? myArr[myArr.length]=[el[i].name,el[i].value] : null;
 //return myArr;
 //document.getElementById('test').innerHTML = myArr;

follow me on twitter @PHPsycho

#2 effigy

  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 04 September 2006 - 05:16 PM

If I understand correctly, the end result is that you want to place a value in a file input. This will not work for security reasons; users have to manually click "Browse..." and select a file. You may be able to do this with other languages (ActiveX?), but to my knowledge, javascript will not work. Can you hide the children instead of removing them?
Regexp | Unicode Article | Letter Database

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users