logo
down
shadow

Clone div with all nested elements along with events in pure javascript


Clone div with all nested elements along with events in pure javascript

By : user2957571
Date : November 24 2020, 01:01 AM
Does that help The simplest (and the most effective) thing to do is to bind single event listener on the #sheet container and benefit from event bubbling. In this case you can append as many new elements as you wish and will never need to bind events to them:
code :
document.querySelector('#sheet').addEventListener('click', function(e) {
    var target = e.target;
    if (target.tagName == 'INPUT') {
        alert("clicked");
    }
}, false);


Share : facebook icon twitter icon
jQuery: clone elements AND events

jQuery: clone elements AND events


By : Dale Funes
Date : March 29 2020, 07:55 AM
this one helps. .clone( true ) does the trick.
Documentation: http://api.jquery.com/clone/
JavaScript/jQuery events not firing on elements within nested <divs>

JavaScript/jQuery events not firing on elements within nested <divs>


By : Inacent Ltd
Date : March 29 2020, 07:55 AM
wish helps you I'm guessing since this is an RSS feed, this data is loaded dynamically? If so, consider wiring these events with jQuery's on
code :
$(".overscroll").on("hover", ".feed-item-container", function() {
});
$(".overscroll").delegate(".feed-item-container", "hover", function() {
});
Clone Div elements and attach events to new elements

Clone Div elements and attach events to new elements


By : Gökhan Üstüner
Date : March 29 2020, 07:55 AM
seems to work fine You are using id with select box and making duplicates of it, this is causing problem to identify select box using id in jQuery. So use class instead of id.
HTML :
code :
<select name="select1" class="select1">
  <option value="1">Fruit</option>
  <option value="2">Animal</option>
  <option value="3">Bird</option>
  <option value="4">Car</option>
</select>

<select name="select2" class="select2">
  <option value="1">Banana</option>
  <option value="1">Apple</option>
  <option value="1">Orange</option>
  <option value="2">Wolf</option>
  <option value="2">Fox</option>
  <option value="2">Bear</option>
  <option value="3">Eagle</option>
  <option value="3">Hawk</option>
  <option value="4">BWM</option>
</select>
  $("#add_a").on('click',function(){
        var data = $('.old_div').html();
        $('.new_div').append(data);
        // to show all options initially and remove any preselected option
        $('.new_div').find('.select2:last option').show().removeAttr('selected');
    });

$(document).on('change',".select1", function() { 
    var id = $(this).val().trim();
    $(this).next('.select2').find('option').hide();
    var $options = $(this).next('.select2').find('option[value=' + id + ']');
   $options.show();
   $options.first().attr('selected',true)
});
Clone container for re-use without all events and elements attached later

Clone container for re-use without all events and elements attached later


By : user2295032
Date : March 29 2020, 07:55 AM
Hope this helps You need to clone data and events both while cloning, please follow below code:
this.container = container;
code :
this.originalContainer = container.clone(true, true);

clone( [dataAndEvents] [, allDataAndEvents] )
javascript clone a tree of elements and change all the IDs in the clone

javascript clone a tree of elements and change all the IDs in the clone


By : user2836719
Date : March 29 2020, 07:55 AM
This might help you Generally, this solution is a 2-step process:
manipulate existing parties one by one from the last to the insert point. insert new party to the insert point.
code :
function add_party(divId) {
    var party = document.getElementById(divId);
    var newParty = party.cloneNode(true);
    var allParties = document.querySelectorAll('div[id^="party"]');
    var newId = parseInt(divId.replace('party', ''), 10) + 1;

    for(var i = allParties.length-1; i > newId-2; i--) {
        allParties[i].setAttribute('id', 'party' + (i+2));
        allParties[i].querySelector('h1').innerHTML = 'Party ' + (i+2);
        var partyInputs = allParties[i].querySelectorAll('input[type="text"]');
        for(var j = 0; j < partyInputs.length; j++) {
            var prefix = partyInputs[j].getAttribute("name").replace(/\d+/, "");
            partyInputs[j].setAttribute("name", prefix + (i+2));
        }
    }

    newParty.setAttribute('id', 'party' + newId);
    newParty.querySelector('h1').innerHTML = 'Party ' + newId;
    var inputs = newParty.querySelectorAll('input[type="text"]');
    for(var i = 0; i < inputs.length; i++) {
        var prefix = inputs[i].getAttribute("name").replace(/\d+/, "");
        inputs[i].setAttribute("name", prefix + newId);
        inputs[i].setAttribute("value", "");
        inputs[i].value = "";
    }

    party.parentNode.insertBefore(newParty, party.nextSibling);
}
<div id="party1">
    <h1>Party 1</h1>
    <table>
        <tbody>
            <tr>
                <th>party name:</th>
                <td><input type="text" name="name1" value="some name"></td>
            </tr>
            <tr>
                <th>party time:</th>
                <td><input type="text" name="time1" value="some time"></td>
            </tr>
        </tbody>
    </table>
    <input type="button" onclick="add_party(this.parentNode.id)" value="add party">
</div>
Related Posts Related Posts :
  • angular 6 Please add a @NgModule annotation
  • Trying to cast element ID to class selector in jQuery
  • Swipe JS - Display 3 Slides at a time
  • Jasmine test to call function when value is null
  • Redux reducer gets string instead of object
  • Keep chrome javascript running even if page reload
  • The event.target.value is not insert the userinput into a variable
  • Exporting HTML tables to Excel (.xls) in a separate sheet
  • Jquery tooltip on dialog close button
  • access variable inside anonymous function from outside
  • How come func is called from a function?
  • How to change add and remove active class in JavaScript
  • how to toggle a specific div in javascript
  • How to get all the values from multiple keys of an array of object?
  • jQuery bind() unbind() and on() and off()
  • regular expression to find DD Month YYYY from a string
  • How to upload files from web client (HTML/jQuery form) to your Dropbox folder
  • Backbone history and require.js issue
  • Confusion over Classical and Prototypal Inheritance Javascript
  • How to take screen shot of current webpage using Javascript/JQuery?
  • Store meridiem in a variable
  • class declared inside closure vs standard class without closure
  • javascript confirm box always return true
  • dynamically changing the size of font size based on text length using css and html
  • Node.js - Getting the host IP address while on Amazon EC2
  • Confusing CSS, can anyone explain please
  • How to install v8-profiler on Windows 8 (64 bit) using npm?
  • Resolve promise based on another promise
  • google maps adding overlay layer above UI and markers
  • Style specific letter?
  • RangeError: Maximum call stack size exceeded with array.slice
  • node rest client get with local variable
  • If condition not executes while mouse move fastly
  • Count the number of displayed elements in a HTML list
  • Locate JavaScript source code in Emacs
  • Clean, Modular Code vs MV* Frameworks
  • infinite scroll without loading image
  • Backbone: reverse collection order with comparator
  • What do printers ignore?
  • jQuery UI .tabs() Contentless tab?
  • Execute Javascript alert() after page (visually) loaded
  • JavaScript - duplicating array doesn't work
  • Excessive clickable area below image
  • JavaScript Regex: Replace |b| with <b>
  • Unexpected value change in 2D array in JavaScript
  • Function doesnt see parameter as a array of objects
  • jQuery fetch keypress event on chrome and IE
  • How to enable jquery validate localization?
  • Cassandra map collection returned by node.js Helenus looks odd
  • angular ng-repeat with multiple filter options
  • Selecting Children without ID or Class names
  • How to uncheck a group of checkboxes when another checkbox is checked
  • Is hiding content by Javascript or jQuery Worth trying
  • Load XUL resource using javascript
  • XML to HTML text area from server file system
  • set focus() on textbox on form onload
  • es lint '' is assigned a value but never used' eventhough i have used it
  • Can "name" attribute be used for custom VueJS components
  • Get innerHTML of content when it is clicked
  • HTTPS causes CSS animations to not load? Very confused
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org