Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    zfs
    zfs is offline
    New to the CF scene
    Join Date
    Feb 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding new elements to a HTML element array

    I have (for example) a HTML elements array that looks like this:
    Code:
    <input type="hidden" name="car[]" value="VW">
    <input type="hidden" name="car[]" value="Audi">
    <input type="hidden" name="car[]" value="Ferrari">
    <input type="hidden" name="car[]" value="Dodge">
    What I need to do is to insert a new element into that array, but in the middle - NOT in the end. It means that the array order and indexes are important. The result should look like this:
    Code:
    <input type="hidden" name="car[]" value="VW">
    <input type="hidden" name="car[]" value="Audi">
    <input type="hidden" name="car[]" value="A BRAND NEW CAR!!!!!">
    <input type="hidden" name="car[]" value="Ferrari">
    <input type="hidden" name="car[]" value="Dodge">

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Not too sure about this

    PHP Code:
    <div id="d1">

    <
    input type="text" name="car[]" value="VW">
    <
    input type="text" name="car[]" value="Audi">
    <
    input type="text" name="car[]" value="Ferrari">
    <
    input type="text" name="car[]" value="Dodge">

    </
    div>

    <
    script type="text/javascript">

    divEl=document.getElementById("d1")
    inputEl=divEl.getElementsByTagName("INPUT")


    if(
    inputEl.length%2==0){
    half=(inputEl.length/2)+1
    }
    else{
    half=Math.ceil(inputEl.length/2)
    }

    newEl=document.createElement("INPUT")
    newEl.setAttribute("type","text")
    newEl.setAttribute("name","car[]")
    newEl.setAttribute("value","A BRAND NEW CAR")

    divEl.insertBefore(newEl,divEl.childNodes[half])

    </script> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    If you can guarantee sequential structure like that:
    Code:
    var cars = document.getElementsByName("car[]");
    var middle = cars[Math.floor(cars.length/2)];
    var cloned = middle.cloneNode(true);
    cloned.value = "something new";
    middle.parentNode.insertBefore(cloned, middle);

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    jkd

    That is neat

    So simple, yet so right

    I later realised that mine produced different results in IE and Firefox

    I must look more into this cloning myself

    Great stuff jkd
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    zfs
    zfs is offline
    New to the CF scene
    Join Date
    Feb 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Thanks jkd! It's almost too good to be true


  •  

    Posting Permissions

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