How to delete created element via JQUERY? - jquery-selectors

$(function(){
var namecounter = 0
$("#getitem").click(function(){
namecounter++
var txtval = $("#txt").val();
$('<tr><td><input type="button" name="txt'+ namecounter +'" value="remove" /><input type="text" id="txt'+ namecounter +'a" name="txt'+ namecounter +'a" value="'+txtval+'"/></td><td></td><td></td></tr>').appendTo('#pasok');
});
$(":button[name^='txt']").click(function(){
var currentValue = $(this);
var target = $("#" + currentValue.attr("id") + "a");
$(target).remove();
});
});
How to delete created element via JQUERY?

Your button doesn't have an id attribute, it has a name attribute (both of which are also a directly accessible DOM properties), so you can do this:
$(function(){
var namecounter = 0
$("#getitem").click(function(){
namecounter++
var txtval = $("#txt").val();
$('<tr><td><input type="button" name="txt'+ namecounter +'" value="remove" /><input type="text" id="txt'+ namecounter +'a" name="txt'+ namecounter +'a" value="'+txtval+'"/></td><td></td><td></td></tr>').appendTo('#pasok');
});
$("#pasok").delegate(":button[name^='txt']", "click", function(){
$("#" + this.name + "a").remove();
});
});
You can test it out here.
Also note the use of .delegate() so it works on the buttons you're adding, not just the ones that were there when your document.ready function ran.

your target variable is already a jquery object. So you should change $(target).remove(); to target.remove();

Related

Google Apps Script - do not send email for checkbox is unchecked

I have the following HTML file written in the script editor within google sheets:
<body>
<form>
<div class="even group">
<input type="text" id="yourName" class="contactNameInput" name="yourName" placeholder="Your name">
<input type="text" id="yourPosition" class="contactNameInput" name="yourPosition" placeholder="Your position">
</div>
<div class="odd group">
<input type="checkbox" id="check1" class="check" checked>
<input type="text" id="name1" class="contactNameInput" name="toAddress1">
<input type="text" id="contactName1" class="contactNameInput mailName" name="contactName1">
<input type="text" id="time1" class="contactNameInput hidden mailTime" name="time1">
<input type="text" id="day1" class="contactNameInput hidden mailDay" name="day1">
<input type="text" id="date1" class="contactNameInput hidden mailDate" name="date1">
<textarea class="additional contactNameInput" id="additional1" name="additional1" placeholder="Additional requests..."></textarea>
<div class="preview1"></div>
</div>
<div class="even group">
<input type="checkbox" id="check2" class="check" checked>
<input type="text" name="toAddress2" id="name2" class="contactNameInput">
<input type="text" id="contactName2" class="contactNameInput mailName" name="contactName2">
<input type="text" id="time2" class="contactNameInput hidden mailTime" name="time2">
<input type="text" id="day2" class="contactNameInput hidden mailDay" name="day2">
<input type="text" id="date2" class="contactNameInput hidden mailDate" name="date2">
<textarea class="additional contactNameInput" id="additional2" name="additional2" placeholder="Additional requests..."></textarea>
<div class="preview1"></div>
</div>
// ... there are 33 of these objects in total - all identical except for the ascending Ids...
<div class="odd group">
<input type="checkbox" id="check33" class="check" checked>
<input type="text" name="toAddress33" id="name33" class="contactNameInput">
<input type="text" id="contactName33" class="contactNameInput mailName" name="contactName33">
<input type="text" id="time33" class="contactNameInput hidden mailTime" name="time33">
<input type="text" id="day33" class="contactNameInput hidden mailDay" name="day33">
<input type="text" id="date33" class="contactNameInput hidden mailDate" name="date33">
<textarea class="additional contactNameInput" id="additional33" name="additional33" placeholder="Additional requests..."></textarea>
<div class="preview1"></div>
</div>
<button type="submit" class="btn btn-primary googleGreen" id="load" data-loading-text="<i class='fa fa-spinner fa-spin'></i> Sending">Invite hotels</button>
</form>
<script>
$(".additional").focus(function(){
$('.dearName').html(function() {
return $(this)
.closest('.preview1')
.siblings('.mailName')
.val();
});
$('.meetingDay').html(function() {
return $(this)
.closest('.preview1')
.siblings('.mailDay')
.val();
});
$('.meetingTime').html(function() {
return $(this)
.closest('.preview1')
.siblings('.mailTime')
.val();
});
$('.meetingDate').html(function() {
return $(this)
.closest('.preview1')
.siblings('.mailDate')
.val();
});
$(this).siblings('div:first').slideDown();
}).blur(function() {
$(this).siblings('div:first').slideUp();
});
$(".additional").keyup(function() {
$(this).next('div').find('.addedText').html($(this).val());
});
$(".preview1").html("<p> Dear <span class='dearName'></span></p> <br> <p>Please can we meet on <span class='meetingDay'></span> <span class='meetingDate'></span> at <span class='meetingTime'></span>.</p><br><p><span class='addedText'></span>If you could kindly let me know if you are able to confirm that would be great.</p><br><p>Many thanks and I look forward to hearing from you soon.</p><br><p>Yours sincerely,</p>");
// $(".dearName").html($(".dearName").prev('.preview1').siblings().find('.mailName')val());
var idArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33];
var hotelName = "";
var hotelAddress = "";
var hotelContact = "";
var hotelTel = "";
var hotelEmail = "";
function onSuccess(test){
// var hotelArray = test;
for(var i=0; i<idArray.length; i++){
hotelName = test[i].name;
hotelAddress = test[i].address;
hotelContact = test[i].contact;
hotelTel = test[i].tel;
hotelEmail = test[i].email;
time = test[i].time;
day = test[i].day;
date = test[i].date;
$("#name" + idArray[i]).val(hotelEmail);
$("#contactName" + idArray[i]).val(hotelContact);
$("#time" + idArray[i]).val(time);
$("#day" + idArray[i]).val(day);
$("#date" + idArray[i]).val(date);
}
} google.script.run.withSuccessHandler(onSuccess).findHotel();
window.onload = function() {
document.getElementsByTagName('form')[0]
.addEventListener('submit', function(e) {
e.preventDefault();
google.script.run
.withSuccessHandler(function(result) {
google.script.host.close()
})
.withFailureHandler(function(result) {
console.log("f %s", result)
})
//.withFailureHandler(function(result){toastr.error('Process failed', result)})
.sendEmail(e.currentTarget);
})};
$('.btn').on('click', function() {
var $this = $(this);
$this.button('loading');
setTimeout(function() {
$this.button('reset');
}, 15000);
});
$(".check").click(function(){
$(this).parent().toggleClass("checkDisabled");
$(this).siblings().toggleClass("disabledInput");
if($(this).siblings().hasClass("disabledInput")) {
$(this).siblings().attr("disabled", true);
} else {
$(this).siblings().attr("disabled", false);
}})
$(this).siblings().attr("disabled", true);
$('.dearName').html(function() {
return $(this)
.closest('.preview1')
.siblings('.mailName')
.val();
});
</script>
</body>
Each of the class 'group' divs within the form represents one client and I have a .gs file which then sends an email on submit to all clients so long as there is an email address in the relevant field:
function sendEmail(form) {
const sSheet = SpreadsheetApp.getActiveSpreadsheet();
const file = DriveApp.getFileById(sSheet.getId());
const documentUrl = file.getUrl();
/* var toEmail = form.toAddress;
var ccEmail = form.ccAddress;
var fromEmail = "****#****.com";
var subject = form.subject;
var message = form.message; */
var toEmail = "";
var fromEmail = "****#****.com";
var message = "";
var hotelAddresses = [
form.toAddress1,
form.toAddress2,
form.toAddress3,
form.toAddress4,
form.toAddress5,
form.toAddress6,
form.toAddress7,
form.toAddress8,
form.toAddress9,
form.toAddress10,
form.toAddress11,
form.toAddress12,
form.toAddress13,
form.toAddress14,
form.toAddress15,
form.toAddress16,
form.toAddress17,
form.toAddress18,
form.toAddress19,
form.toAddress20,
form.toAddress21,
form.toAddress22,
form.toAddress23,
form.toAddress24,
form.toAddress25,
form.toAddress26,
form.toAddress27,
form.toAddress28,
form.toAddress29,
form.toAddress30,
form.toAddress31,
form.toAddress32,
form.toAddress33,
];
var contactNames = [
form.contactName1,
form.contactName2,
form.contactName3,
form.contactName4,
form.contactName5,
form.contactName6,
form.contactName7,
form.contactName8,
form.contactName9,
form.contactName10,
form.contactName11,
form.contactName12,
form.contactName13,
form.contactName14,
form.contactName15,
form.contactName16,
form.contactName17,
form.contactName18,
form.contactName19,
form.contactName20,
form.contactName21,
form.contactName22,
form.contactName23,
form.contactName24,
form.contactName25,
form.contactName26,
form.contactName27,
form.contactName28,
form.contactName29,
form.contactName30,
form.contactName31,
form.contactName32,
form.contactName33,
];
var days = [
form.day1,
form.day2,
form.day3,
form.day4,
form.day5,
form.day6,
form.day7,
form.day8,
form.day9,
form.day10,
form.day11,
form.day12,
form.day13,
form.day14,
form.day15,
form.day16,
form.day17,
form.day18,
form.day19,
form.day20,
form.day21,
form.day22,
form.day23,
form.day24,
form.day25,
form.day26,
form.day27,
form.day28,
form.day29,
form.day30,
form.day31,
form.day32,
form.day33,
];
var dates = [
form.date1,
form.date2,
form.date3,
form.date4,
form.date5,
form.date6,
form.date7,
form.date8,
form.date9,
form.date10,
form.date11,
form.date12,
form.date13,
form.date14,
form.date15,
form.date16,
form.date17,
form.date18,
form.date19,
form.date20,
form.date21,
form.date22,
form.date23,
form.date24,
form.date25,
form.date26,
form.date27,
form.date28,
form.date29,
form.date30,
form.date31,
form.date32,
form.date33,
];
var times = [
form.time1,
form.time2,
form.time3,
form.time4,
form.time5,
form.time6,
form.time7,
form.time8,
form.time9,
form.time10,
form.time11,
form.time12,
form.time13,
form.time14,
form.time15,
form.time16,
form.time17,
form.time18,
form.time19,
form.time20,
form.time21,
form.time22,
form.time23,
form.time24,
form.time25,
form.time26,
form.time27,
form.time28,
form.time29,
form.time30,
form.time31,
form.time32,
form.time33,
];
var additionalInfo = [
form.additional1,
form.additional2,
form.additional3,
form.additional4,
form.additional5,
form.additional6,
form.additional7,
form.additional8,
form.additional9,
form.additional10,
form.additional11,
form.additional12,
form.additional3,
form.additional14,
form.additional15,
form.additional16,
form.additional17,
form.additional18,
form.additional19,
form.additional20,
form.additional21,
form.additional22,
form.additional23,
form.additional24,
form.additional25,
form.additional26,
form.additional27,
form.additional28,
form.additional29,
form.additional30,
form.additional31,
form.additional32,
form.additional33,
];
for(var i = 0; i<times.length; i++){
var subject = "Meeting - " + days[i] + ", " + dates[i] + " at " + times[i];
toEmail = hotelAddresses[i];
message = "Dear " + contactNames[i] + ","
+"<br><br>"+
"Please can we meet on " + days[i] + " " + dates[i] + " at " + times[i] + "." + "<br>" + "<br>" +
additionalInfo[i] +
" If you could kindly let me know if you are able to confirm that would be great." + "<br>" + "<br>" +
"Many thanks and I look forward to hearing from you soon." + "<br>" + "<br>" +
"Yours sincerely," + "<br>" + "<br>" +
form.yourName + "<br>" + "<br>"
+ "<em><b>" + form.yourPosition + "</b></em> <br><br>" +
"<span style='color:#0e216d'><b> Company name </b>" + "<br>" +
"Company address</span><br>" +
"<img src='companylogo.jpg' style='width: 50%; margin-top: 10px'>";
if(toEmail) {
GmailApp.sendEmail(
toEmail, // recipient
subject, // subject
'test', { // body
htmlBody: message // advanced options
}
);
}}
}
However, as well as not sending an email when there is no address entered, I need to stop the email from sending when the checkbox is not checked. I'm not quite sure where to start with this...
A bit of restructuring of your code is probably going to help you a lot here. For example, the hotelAddresses array is currently a manually created list of addresses - hard to maintain. I'd begin by using the group class on all of the divs to your advantage, as you can select every single one, in order, using the document.getElementsByClassName("group") selector. This will return an array of all your group elements.
Now that we have an array of all these groups, we can process them in a much more concise way. For example, creating that hotel addresses array becomes as simple as:
var hotelAddresses = [], groupElements = document.getElementsByClassName("group")
for (var i = 1; i < groupElements.length; i++) {
var isChecked = groupElements[i].getElementsByClassName("check")[0].checked
var address = groupElements[i].getElementsByClassName("contactNameInput")[0].value
if (isChecked && address != "") { // Add if is checked and has an address
hotelAddresses.push(address)
}
}
You can easily add the code for the contactNames and other arrays in to this same loop. Maybe even another data structure which is an array of your groups for much easier access.
var groups = []
... // Loop code
groups.push({
address: groupElements[i].getElementsByClassName("contactNameInput")[0].value,
contactName: ...
})
You get the idea! Hope this helps you out a bit.

i want to set counter which automatically increment and decrements the value

I want to set counter which automatically ++ the value of name like this Email1 Email2 and so on by clicking add field button and further when i click on the remove button it should -- the value of Email in reverse like Email2 Email1 and so on below I'm using the code,
<div class="input_fields_wrap">
<div class="form-group">
<label>Email</label>
<button class="add_field_button">Add More Fields</button>
<input type="text" class="form-control" name="Email" value="<?php echo htmlspecialchars($team->email);?>" />
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 20; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="Email"/>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
Email is the name of the text field and i want to to increment or decrement this name
Just changed following code:
<input type="text" class="del" id="' + name + '"/>
and
$(".del").last().parent('div').remove();
it will always remove the last element.
Following is the working code Just set the value of text box:
$(document).ready(function() {
var max_fields = 20; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
var name = "Email" + x;
$(wrapper).append('<div><input type="text" class="del" id="' + name + '"/>Remove</div>'); //add input box
}
});
$(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
var removeLastName = "Email" + x;
e.preventDefault(); $(".del").last().parent('div').remove();
x--;
})
});

Liferay alloy button enable/disable

I am trying to get a simple button to be enabled/disabled when checkboxes are being selected, yet in Alloy UI within Liferay, it doesnt seem to work. Any suggestions?
<aui:form>
<aui:input checked="<%= true %>" cssClass="input-container" label="Decline" name="termsOfServiceRadio" type="radio" onClick='<%= renderResponse.getNamespace() + "disableCheckout();"%>'/>
<aui:input cssClass="input-container" label="Accept" name="termsOfServiceRadio" type="radio" onClick='<%= renderResponse.getNamespace() + "enableCheckout();"%>'/>
<aui:button-row>
<aui:button type="submit" name="submitButton" id="submitButtonID" disabled="true" />
</aui:button-row>
</aui:form>
<aui:script>
function <portlet:namespace />enableCheckout() {
document.<portlet:namespace />fm.<portlet:namespace />.getElementById("submitButtonID").disabled = false;
}
function <portlet:namespace />disableCheckout() {
document.<portlet:namespace />fm.<portlet:namespace />.getElementById("submitButtonID").disabled = true;
}
</aui:script>
To continue my trials with no success:
<aui:script>
function <portlet:namespace />enableCheckout() {
var mySubmittButton = A.one('#<portlet:namespace />submitButton');
mySubmittButton.set('disabled', false);
mySubmittButton.ancestor('.aui-button').removeClass('aui-button-disabled');
}
</aui:script>
<aui:script use="aui-base">
function <portlet:namespace />enableCheckout() {
var A = AUI();
var myBtn = A.one('.submitVisible-button');
myBtn.one(':button').attr('disabled', false);
myBtn.toggleClass('aui-button-disabled', false);
}
</aui:script>
<
<aui:button type="submit" name="submitButtonID" id="submitButtonID" cssClass="submitVisible-button" disabled="true" />
<aui:script use="aui-base">
Liferay.provide(
window,
'<portlet:namespace />enableCheckout',
function() {
var myButton = A.one('#<portlet:namespace />submitButtonID');
Liferay.Util.toggleDisabled(myButton, 'true');
myButton.set('disabled', false);
myButton.ancestor('.aui-button').removeClass('aui-button-disabled');
});
</aui:script>
<aui:script use="aui-base">
Liferay.provide(
window,
'<portlet:namespace />enableCheckout',
function() {
var A = AUI();
var myButton = A.one('#<portlet:namespace />submitButtonID');
Liferay.Util.toggleDisabled(myButton, true);
});
</aui:script>
So it appears you want to enable/disable submitButtonID based on termsOfServiceRadio.
You have several variations of essentially the same approach available. The basic concept is to assign click listeners to each radio button setting the state of the button based only on the "Accept" option being "checked". You can use either an id attribute to apply the listeners individually or some css class that will allow you to get both input elements at once and apply the listeners through the each method. A third option is to use the delegate function, which would require that you wrap the radio options in a "container".
YUI().use('aui-base', function(A){
var button = A.one('#mySubmitButton');
button.set('disabled', true);
var func = function(){button.set('disabled', !A.one('#AcceptRadioId').get('checked'));}
A.all('.tosRadioOption').each(function(node){
node.on('click', func)
})
})
Ultimately the set method using the disabled property on the submitButtonID node is the key components required to achieve the desired functionality. My fiddle contains the three approaches I mentioned. Considering you are using the aui taglib elements you'll need to prefix the ids with <portlet:namespace /> as you have done in some of your other attempts.
Ended up doing this a different way
<aui:form>
<aui:input checked="<%= true %>" cssClass="input-container" label="Decline" name="termsOfServiceRadio" type="radio" onClick="document.getElementById('test').style.visibility = this.checked ? 'hidden' : 'visible';"/>
<aui:input cssClass="input-container" label="Accept" name="termsOfServiceRadio" type="radio" onClick="document.getElementById('test').style.visibility = this.checked ? 'visible' : 'hidden';" />
<div id="test" style="visibility:hidden;">
<br/>
<strong>Choose a payment method:</strong>
<br/><br/>
<aui:input checked="<%= true %>" cssClass="input-container" label="Pay online with PayPal" name="paymentMethod" type="radio" onClick='<%= renderResponse.getNamespace() + "setPaypal();"%>'/>
<aui:input cssClass="input-container" label="Pay with check or wire transfer" name="paymentMethod" type="radio" onClick='<%= renderResponse.getNamespace() + "setOffline();"%>'/>
<aui:button-row>
<aui:button type="submit" name="submitButtonID" id="submitButtonID" cssClass="submitVisible-button" value='<%= shoppingPrefs.usePayPal() ? "continue" : "finished" %>' />
</aui:button-row>
</div>

Form input fails to submit when in a div

I replicated the Developer's example code with success. When i insert a div into the form it fails. How can i submit 'form div input' to a server side function?
* i believe divs and spans are allowed inside forms from here.
* uncommenting the divs causes the div 'output' not to update.
html:
<form id="myForm">
<!--><div>-->
<input name="myEmail" type="text" />
<input type="submit" value="Submit"
onclick="google.script.run.withSuccessHandler(updateEmail)
.processForm(this.parentNode)" />
<!--></div>-->
</form>
<div id="output">
</div>
<script>
function updateEmail(response) {
var div = document.getElementById("output");
div.innerHTML = "<p>" + response + "</p>";
}
</script>
code.gs
function doGet() {
var html = HtmlService.createTemplateFromFile('index').evaluate()
.setTitle('Web App').setSandboxMode(HtmlService
.SandboxMode.NATIVE);
return html;
}
function processForm(formObject) {
var response = "";
response = formObject.myEmail;
return response;
};
Edit:
changed:
<input type="button" value="Submit"
to:
<input type="submit" value="Submit"
I changed the HTML file to this:
<form id="myForm">
<div>
<input name="myEmail" type="text" />
<input type="button" value="Submit"
onclick="processFormJs(this.parentNode)" />
</div>
</form>
<div id="output"></div>
<script>
window.processFormJs = function(argDivParent) {
console.log('argDivParent: ' + argDivParent);
google.script.run.withSuccessHandler(updateEmail)
.processForm(argDivParent)
};
function updateEmail(response) {
var div = document.getElementById("output");
div.innerHTML = "<p>" + response + "</p>";
}
</script>
And added a console.log('argDivParent: ' + argDivParent); statement. Then in developer tools, show the console. I get this error:
argDivParent: [domado object HTMLDivElement DIV]
Failed due to illegal value in property: 0
this.ParentNode is referring to the DIV and not the FORM. If I take out the DIV, the object returned is:
argDivParent: [domado object HTMLFormElement FORM]
Not:
argDivParent: [domado object HTMLDivElement DIV]
A DIV probably doesn't automatically put INPUT values into it's parent object.
This code does work with a DIV:
<form id="myForm" onsubmit="processFormJs(this)">
<div>
<input name="myEmail" type="text" />
<input type="button" value="Submit"/>
</div>
</form>
<div id="output"></div>
<script>
window.processFormJs = function(argDivParent) {
console.log('argDivParent: ' + argDivParent);
google.script.run.withSuccessHandler(updateEmail)
.processForm(argDivParent)
};
function updateEmail(response) {
var div = document.getElementById("output");
div.innerHTML = "<p>" + response + "</p>";
}
</script>
For debugging, you can use Logger.log("your text here"); then view the Logs.
function processForm(formObject) {
Logger.log('processForm ran: ' + formObject);
var response = "";
response = formObject.myEmail;
Logger.log('response: ' + response);
return response;
};

dynamic creation of textbox

while i create textbox dynamically i can able to add any no
of textbox while i delete only last textbox got deleted after it
an throws exception.
<html>
<head>My page </html>
<body>
<div id="firstdiv">
<input type="text" id="text" id="text1" value=""/>
<input type="button" id="butt" name="it's okay" value="+" onclick="text_add()"/>
<input type="button" id="butt1" name="it's okay" value="-" nclick="text_remove()"/>
</div>
</body>
<script type="text/javascript">
var i=0;
function text_add()
{
++i;
var bal=document.createElement("input")
bal.setAttribute("type","text");
bal.setAttribute("id","text"+i);
bal.setAttribute("name","bala");
firstdiv.appendChild(bal);
}
function text_remove()
{
try{
var a="\"";
a+="text"+(i);
a+="\"";
alert(a);
var bal=document.getElementById(a);
firstdiv.removeChild(bal);
--i;
}catch(e)
{
alert("Echo"+e);
}
}
</script>
</html>
chorme throws error: after deleting one text box like
EchoError: NOT_FOUND_ERR: DOM Exception 8
so you whant to add and remove textboxes.
and the latest textbox added shuld be removed?
so first you alreddy have a textbox in the dom witch will colide with your
naming
<input type="text" id="text" id="text1" value=""/>
is this an atemt to manualy add qoutes ? becus thats not part of the id
of the element
var a="\"";
a+="text"+(i);
a+="\"";
just give it the name normaly
var bal = document.getElementById("text" + i);
and you dont have to mess around with dom ids you can store
dom elements as normal objects so
something like this works fine
var textElements = [];
var firstdiv = document.getElementById("firstdiv");
function text_add() {
var bal = document.createElement("input");
bal.setAttribute("type","text");
bal.setAttribute("name","bala");
firstdiv.appendChild(bal);
textElements.push(bal);
}
function text_remove() {
firstdiv.removeChild(textElements.pop());
}
the pop method removes the last added item in the array