I found many ways to operate on new elements after DOM change but all works with events. I just want on click hide element 1 than replace it with 2 with display:none and show it.
My code so far:
/*
* EDIT COMMENT
*/
jQuery(document).on('click',options.targets.container+' '+options.targets.comment_edit, function(e){
e.preventDefault();
var target = e.target;
target = jQuery(target).parent();
var comment_content = jQuery(target).find('.comment_content');
var comment_content = jQuery(comment_content).text();
jQuery(target).find('.comment_content').hide(400,function(){
jQuery(target).find('.comment_content').replaceWith('<textarea style="display:none" class="'+options.targets.comment_edit_form.replace('.','')+'">'+comment_content+'</textarea>');
//not working
jQuery(target).delay(400).find(options.targets.comment_edit_form.replace('.','')).show(400)
console.log(jQuery(target).find(options.targets.comment_edit_form.replace('.','')))
});
})
I want to show new element just after i do replacing.
Problems are 2:
1: I can't get new element with jquery because it is added;
2: I can't display it after other function is done.
Here's how I created an element:
var dialogDiv = document.createElement("div");
$("body").append(dialogDiv);
$(dialogDiv).html('<p>' + text + '</p>');
Search element after insertion.
Modify your code as
jQuery(document).on('click',options.targets.container+' '+options.targets.comment_edit, function(e){
e.preventDefault();
var target = e.target;
target = jQuery(target).parent();
var comment_content = jQuery(target).find('.comment_content');
var comment_content = jQuery(comment_content).text();
jQuery(target).find('.comment_content').hide(400,function(){
jQuery(target).find('.comment_content').replaceWith('<textarea style="display:none" class="'+options.targets.comment_edit_form.replace('.','')+'">'+comment_content+'</textarea>');
//show after ( put your code here )
jQuery(target).find('textarea').show();
});
})
http://jsbin.com/pirinocuku/edit?html,js,output
What i did wrong was
jQuery(target).find(options.targets.comment_edit_form.replace('.','')).show(400)
options.targets.comment_edit_form is class and i remove . from it so it couldnt find an element;
Changed to:
jQuery(document).on('click',options.targets.container+' '+options.targets.comment_edit, function(e){
e.preventDefault();
var target = e.target;
target = jQuery(target).parent();
var comment_content = jQuery(target).find('.comment_content');
var comment_content = jQuery(comment_content).text();
jQuery(target).find('.comment_content').hide(400,function(){
jQuery(target).find('.comment_content').replaceWith('<textarea style="display:none" class="'+options.targets.comment_edit_form.replace('.','')+'">'+comment_content+'</textarea>');
jQuery(target).find(options.targets.comment_edit_form.replace('.','')).show(400)
});
})
My bad, sorry guys
Related
Let me be clear. I am asking how to change CSS code on a selected text. Not like a checkbox or anything of that sort.
var txtarea = document.getElementById("txtarea");
var selected = window.getSelection();
if (selected) {
//Change CSS Code
}
Thanks in advance.
You can achieve a new styling using an active class, so you will be able to use it in every element with an ID of txtarea by clicking on the element you will toggle between the common txtarea and txtarea active
If you have an element, for example a textarea tag with an ID called txtarea
document.querySelector('#txtarea').addEventListener('click', ctrlAddItem);
Now you have to create a loop:
document.querySelectorAll("#txtarea").forEach(function () {
this.addEventListener("click", ctrlAddItem);
});
Now you have to create a function, so everytime you click on element it will add or remove the ID active:
var ctrlAddItem = function(e) {
e.target.classList.toggle("active");
}
you can do it like this:
var txtarea = document.getElementById("txtarea");
txtarea.style.width = '200px';
txtarea.style.height = '200px';
txtarea.style.color = 'grey';
txtarea.style.fontSize = '18px';
keep in mind that if the selected element doesn't exist in the DOM JavaScript will throw an error, so you don't need to use an if statement to test if it exists
I'm working on a JavaScript project where a user can click a button to create a text element. However, I also want a feature where I can click a different button and the element that was created most recently will be removed, so In other words, I want to be able to click a button to create an element and click a different button to undo that action.
The problem I was having was that I created the element, then I would remove the element using:
element.parentNode.removeChild(element); , but it would clear all of the elements that were created under the same variable.
var elem = document.createElement("div");
elem.innerText = "Text";
document.body.appendChild(elem);
This code allows an element to be created with a button click. All elemente that would be created are under the "elem" variable. so when I remove the element "elem", all element are cleared.
Is there a simple way to remove on element at a time that were all created procedurally?
Thanks for any help
When you create the elements, give the a class. When you want to remove an element, just get the last element by the className and remove it.
The below snippet demonstrates it -
for(let i = 0; i<5; i++){
var elem = document.createElement("div");
elem.innerText = "Text " + i;
elem.className = "added";
document.body.appendChild(elem);
}
setTimeout(function(){
var allDivs = document.getElementsByClassName("added");
var lastDiv = allDivs.length-1;
document.body.removeChild(allDivs[lastDiv]);
}, 3000);
I would probably use querySelectors to grab the last element:
// optional
// this is not needed it's just a random string added as
// content so we can see that the last one is removed
function uid() {
return Math.random().toString(36).slice(2);
}
document.querySelector('#add')
.addEventListener('click', (e) => {
const elem = document.createElement('div');
elem.textContent = `Text #${uid()}`;
document.querySelector('#container').appendChild(elem);
// optional - if there are elements to remove,
// enable the undo button
document.querySelector('#undo').removeAttribute('disabled');
});
document.querySelector('#undo')
.addEventListener('click', (e) => {
// grab the last child and remove
document.querySelector('#container > div:last-child').remove();
// optional - if there are no more divs we disable the undo button
if (document.querySelectorAll('#container > div').length === 0) {
document.querySelector('#undo').setAttribute('disabled', '');
}
});
<button id="add">Add</button>
<button id="undo" disabled>Undo</button>
<div id="container"></div>
I have a function that creates an html element with an unique ID.
And after that I want that when I click this element I could call a new function.
Quick example:
1) I click a button "Create element";
2) An element is created with id of "New_Element";
3) I click the "New_Element";
4) I get a function that was already preset to this element.
My current code for creating an element.
var pageRows = document.getElementsByClassName('pageRows');
var pageRowID = "section";
var el = document.createElement('section');
el.setAttribute('id', pageRowID + pageRows.length);
var row = document.getElementById('allNewRows');
row.parentNode.appendChild(el);
el.innerText = "New " + pageRows.length + " ROW!";
Now that the Element of id "pageRowId0" is created I want to have a function that works when I click this element.
Best wishes.
Thanks for helping.
You can do element.onclick= function(){}
var pageRows = document.getElementsByClassName('pageRows');
var pageRowID = "section";
var el = document.createElement('section');
el.setAttribute('id', pageRowID + pageRows.length);
el.onclick = function(){
/*write your fn here*/
};
var row = document.getElementById('allNewRows');
row.parentNode.appendChild(el);
el.innerText = "New " + pageRows.length + " ROW!";
You can use event delegation:
var row = document.getElementById('allNewRows');
row.parentNode.onclick = function(e) {
if (e.target.nodeName.toLowerCase() == 'select') {
//click on target select element
}
};
The snippet below has two parts. The first piece of code allows you to add a bunch of elements with different texts to the document.
The second parts shows the text of the element you clicked.
You will notice that the click event handler is just assigned to the parent element in which the new elements are added. No explicit click event handlers are bound to the new element.
I like to use addEventListener, because I think it's better to add a listener for a specific goal than to override any other event listeners by bluntly setting 'onclick', but that's a matter of opinion.
// Only run this code when the DOM is loaded, so we can be sure the proper elements exist.
window.addEventListener('DOMContentLoaded', function(){
// The code to add an element when the add button was clicked.
document.getElementById('add').addEventListener('click', function() {
var element = document.createElement('div');
element.innerText = document.getElementById('text').value;
element.className = 'clickableElement';
document.getElementById('elements').appendChild(element);
});
// Click event handler for the 'elements' div and everything in it.
document.getElementById('elements').addEventListener('click', function(event) {
var target = event.target; // The element that was clicked
// Check if the clicked element is indeed the right one.
if (target.classList.contains('clickableElement')) {
alert(target.innerText);
}
});
})
<input id="text" value="test"><button id="add">add</button>
<div id="elements"></div>
I have the following script
var counter = 0;
function appendText(){
var text = document.getElementById('usertext').value;
if ( document.getElementById('usertext').value ){
var div = document.createElement('div');
div.className = 'divex';
var li = document.createElement('li');
li.setAttribute('id', 'list');
div.appendChild(li);
var texty = document.createTextNode(text);
var bigdiv = document.getElementById('addedText');
var editbutton = document.createElement('BUTTON');
editbutton.setAttribute('id', 'button_click');
var buttontext = document.createTextNode('Edit');
editbutton.appendChild(buttontext);
bigdiv.appendChild(li).appendChild(texty);
bigdiv.appendChild(li).appendChild(editbutton);
document.getElementById('button_click').setAttribute('onClick', makeAreaEditable());
document.getElementById('usertext').value = "";
counter++;
}
};
var makeAreaEditable = function(){
alert('Hello world!');
};
I want the makeAreaeditable function to work when the Edit button is pressed(for each of the edit buttons that are appended under the textarea).. In this state, the script, alerts me when i hit the Addtext button.
the following is the html. P.S. i need this in pure javascript, if you can help. thanks
<textarea id="usertext"></textarea>
<button onClick="appendText()">Add text </button>
<div id="addedText" style="float:left">
</div>
instead of:
document.getElementById('button_click').setAttribute('onClick', makeAreaEditable());
you need to do this:
editbutton.onclick = makeAreaEditable;
the function's name goes without brackets unless you want to execute it
instead of obtaining the element from the DOM using document.getElementById('button_click')
you can use the editbutton variable already created. this object is the DOM element you are looking for
SIDE NOTE:
the standard way to do it is to add the onclick property before appending the element
I just try a tutorial here on how to upload multiple form - http://www.maheshchari.com/multifile-upload/
Basically, it have a link to add a new input when it clicked. My question is, how to add another link to REMOVE the input?
Thanks for helping :)
You can remove an element that you know the ID of using:
function removeById(id) {
var element = document.getElementById(id);
// A bit of robustness helps...
if (element && element.parentNode) {
element.parentNode.removeChild(element);
}
}
please update you function add_file_field to
var file_counter = 0;
function add_file_field(){
file_counter++;
var container=document.getElementById('file_container');
var file_field=document.createElement('input');
file_field.name='images[]';
file_field.type='file';
file_field.id='file_'+file_counter;
container.appendChild(file_field);
var remove_field = document.createElement('a');
remove_field.href = "javascript:removeById('"+'file_'+file_counter+"');removeById('"+'remove_field_'+file_counter+"');";
remove_field.innerHTML = "Remove')";
remove_field.id = 'remove_field_'+file_counter;
var br_field=document.createElement('br');
container.appendChild(br_field);
}
this create a
and also add function removeById in you javascript so that when any one clicks on remove button then the file type field will remove. which is posted in previous post also
function removeById(id) {
var element = document.getElementById(id);
// A bit of robustness helps...
if (element && element.parentNode) {
element.parentNode.removeChild(element);
}
}
It's very simple,
document.getElementById("_id").parentNode.removeChild(document.getElementById("_id"));