Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Inserting the same code for use as a block of elements

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #44592
    fooman
    Participant

    I have an “Add” button that will add a group of fields to a list/form using JQuery.
    What’s the best way to do this?

    Do you set up a hidden group on the page and use Jquery to clone() it, append to the list, and then remove the class that is hiding it?

    Or do you code this group of content in Javascript somehow and build it each time you want to add a new group to the page?

    #134381
    JohnMotylJr
    Participant

    Use this as a little reference: http://codepen.io/johnmotyljr/pen/FtLxu

    I took a snippet from this site and basically hide the first element and copied the rest.

    #134384
    CrocoDillon
    Participant

    Personally I would build the whole thing in JS. I sort of like John’s implementation with a ‘prototype’ node, and it seems to do a better job of separating markup and script… but it still feels hacky to me and it’s weird to have markup for content that shouldn’t be there yet.

    #134390
    JohnMotylJr
    Participant

    Ya know what, that is a good call. Build the elements with js, cache em, and duplicate. Afterwards set element attributes.


    @fooman
    , do you have some markup we could look at? Crocodillons implementation wouldn’t be hard at all.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.