Jump to content

Regualr expressions and array creation


ToonMariner

Recommended Posts

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:

[code]
<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]" /.
<div>
...
</div>
[/code]

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.

i.e.

if i had the follwoing 2 child divs:
[code]
<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>
<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]" /.
<div>
</div>
[/code]

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....

[code]
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;
    break;
    case 'textarea':
    myArr[$2][$1] = el[i].innerHTML;
    break;
    default:
  }
  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;
}
[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.