Let's suppose: a page with some dynamically generated checkboxes outside of a form element.
After the user has checked some of the checkboxes, I would love to append all those checkboxes (either checked or unchecked) into the form element so that when the user click the "submit" button, the form takes into account the checkboxes, their ids, names, data-names and their status (checked or unchecked). Is that possible ?
I have tried a codpen here: https://codepen.io/anthonysalamin/pen/ZxvZpP?editors=1010 but was unsuccessful sofar.
The jQuery code:
//insert all checkbox input elements into the form id="reservation"
$("#reservation").submit(function(evt) {
// should append all checkboxes to the form
$("<input type='checkbox' />").append("#reservation");
});
Screenshot of my codpen here
.append() expects textual HTML as input. You have passed the id selector of #reservation instead. To effectively add the checkboxes to the form in the submit event you could do this:
// wait for DOM to be ready
$( document ).ready(function() {
//define the variable for all checkboxes on the page
var allCheckboxes = $("input:checkbox[class=outsider]");
//calls the append function on form submission
$("#form-element").submit(function(event) {
//insert all checkbox type elements into the form with id="form-element"
var checkHTML = "";
for(var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
checkHTML += allCheckboxes[checkboxIndex].outerHTML;
}
$("#form-element").append(checkHTML);
});
});
However, it is highly probable that your intention differs from the behavior. Problems with the code:
your checkboxes have ids and if we clone them, then you will have duplicate ids. Since an HTML cannot be valid if there are duplicate ids, cloning the checkboxes with ids will result in invalid HTML
checked state is not copied, so you will need to mine that value out and put it to the newly created checkboxes
Code dealing with all these problems:
// wait for DOM to be ready
$( document ).ready(function() {
//define the variable for all checkboxes on the page
var allCheckboxes = $("input:checkbox[class=outsider]");
//calls the append function on form submission
$("#form-element").submit(function(event) {
//insert all checkbox type elements into the form with id="form-element"
var checkHTML = "";
var checked = [];
for(var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
checked.push($(allCheckboxes[checkboxIndex]).prop('checked'));
allCheckboxes[checkboxIndex].remove();
checkHTML += allCheckboxes[checkboxIndex].outerHTML;
}
$("#form-element").append(checkHTML);
allCheckboxes = $('input:checkbox[class=outsider]');
console.log(checked);
for (var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
$(allCheckboxes[checkboxIndex]).prop('checked', checked[checkboxIndex]);
}
event.preventDefault();
});
});
So you can solve your problem by cloning the checkboxes if you handle all the problems. Alternatively, you could use a hidden input where you could put key-value pairs, where the keys will be checkbox ids and values will be their corresponding checked states and put those values into the hidden input at the submit handler. If you do not specifically intend to visually put the checkboxes into the form and you are satisfied with correct handling of data, then putting the JSON value of key-value pairs into the value of a hidden input is superior in comparison of copying checkboxes into the form, but these are only nuances.
Related
HTML
<ion-input [(ngModel)]="login.username" ngControl="username1" type="number" #username1="ngForm" id="userName" required>
</ion-input>
PROTRACTOR TEST CODE
let usern: ElementFinder = element.all(by.css('.text-input')).get(0);
usern.sendKeys('error');
expect(usern.getAttribute("value")).toEqual("error");
browser.sleep(500);
usern.clear();
browser.sleep(1000);
usern.sendKeys('12345');
The element is found but no text is entered into the field. If I change the element to type="text" the protractor command works.And the page view is 'e' and can't be clear.
Secondly if I send string like this: "we2124will", the actually send data is '2124' and the result from getAttribute("value") is 2124.
Thirdly even if I changed the sendKeys to number, the result is not full number string. For example:
Failures:
1) Login page should input username and password
Message:
Expected '125' to equal '12345'.
Stack:
Error: Failed expectation
There are some number missing.
Since you're using an <ion-input>, the actual HTML <input> tag will be nested within, and it won't have an id attribute. The effect is that the wrong element can get selected.
Try something like below to grab the nested input tag:
let username = element(by.id('userName')).all(by.tagName('input')).first();
username.sendKeys('fakeUser');
That worked for me.
As a workaround, you can introduce a reusable function that would perform a slow type by adding delays between send every key.
First of all, add a custom sleep() browser action, put this to onPrepare():
protractor.ActionSequence.prototype.sleep = function (delay) {
var driver = this.driver_;
this.schedule_("sleep", function () { driver.sleep(delay); });
return this;
};
Then, create a reusable function:
function slowSendKeys(elm, text) {
var actions = browser.actions();
for (var i = 0, len = text.length; i < len; i++) {
actions = actions.sendKeys(str[i]).sleep(300);
}
return actions.perform();
}
Usage:
var elm = $("ion-input#userName");
slowSendKeys(elm, "12345");
What version of protractor are you using?
Not sure this is the issue but try grabbing the element by ng-model
var elem = element(by.model('login.username'));
elem.sendKeys('error');
expect(elem.getAttribute("value")).toEqual("error");
elem.clear();
elem.sendKeys('12345');
expect(elem.getAttribute("value")).toEqual("12345");
I have a google spreadsheet which contains multiple sheets (or tabs) within it. Each sheet is populated from its own unique form. None of the forms are embedded in the spreadsheet.
Periodically, I need to delete all the data in the sheets, and also delete all the old responses which are saved in each of the forms. I can do this using a .gs script which resides in the spreadsheet. It accesses the form by its ID (the long string which appears in its URI). This requires the ID string to be hardcoded in my .gs script.
Ideally, I would like to access each form from the sheet object (i.e. the destination for each forms entries). Mock up code would look like this...
var ss = SpreadSheedApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var form = sheet.getMyAssociatedSourceForm(); // my dream method :-)
form.deleteAllResponses() // this method already exists
Does anyone know if this is possible? Or will I have to continue to use the ID (which is currently working)?
rgds...
I think you can do this without literally typing in ID's into your script. But, you would need to get every Form in your drive, loop through them all and get the destinationId() of every Form.
Google Documentation
Then compare the destinationId with the current spreadsheets ID, which you can get without needing to "hard code" it:
function deleteAllResponses() {
var thisSS_ID = SpreadsheetApp.getActiveSpreadsheet().getId();
var allForms = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
var thisFormFile, thisFormFileID = "", thisForm, theDestID = "";
while (allForms.hasNext()) {
thisFormFile = allForms.next();
thisFormFileID = thisFormFile.getId();
thisForm = FormApp.openById(thisFormFileID);
try {
theDestID = thisForm.getDestinationId();
} catch(err) {
continue;
};
if (theDestID === "" || theDestID === undefined) {
continue;
};
if (theDestID === thisFormFileID) {
thisForm.deleteAllResponses();
};
};
};
I have not tested this, so don't know if it works. If it does, let me know in the comments section.
I have a form in as3 flash with textfields and radio buttons with their "instance name" all set consecutively such as:
field_1
field_2
field_3
...etc
Is there an easy way in AS3 code to loop over all those fields and get their values in an array? Ideally I'd like a simply loop to get the values (so I can add in a simple popup if a value is missing) and then with the filled array simply post it using URLRequest.
Thanks!
If you want a way that's a little less work and more maintainable (if the amount of text can change in the future or you need to reuse the code on other forms or don't want to bother with instance names), then you could do something like the code below.
Keep in mind this assumes all your text fields are children of the same parent and is in the scope of that parent (so on a frame in the timeline that holds all your text fields)
function validateTextFields(){
var tmpTf:TextField;
var i:int = numChildren;
while(i--){ //iterate through all the display objects on this timeline
tmpTf = getChildAt(i) as TextField;
if(tmpTf){
//now that you have the textfield, you can check for an appropriate value, or send the value to a server, or store it in an array etc.
//check if the value is blank, if so set the background to red
if(tmpTf.text == ""){
tmpTf.background = true;
tmpTf.backgroundColor = 0xFF0000;
}else{
tmpTf.background = false;
}
}
}
}
It sounds like you want to use a for statement. I've also used a multi-dimensional array to store the instance names, and the text that they contain. I like to use the variable _path to define the scope of my code.
var _path = this;
var text_ar:Array = new Array(['instance_1',''],['instance_2',''],['instance_3','']); //instance name, instance text.
for(var i=0; i<ar.length; i++)
{
text_ar[i][1] = _path[text_ar[i][0]].text
}
trace( text_ar[1][1] ); //Traces the text that's entered in instance_1.
Below is the prototype of what I am trying to do.
var entry_set = $;
// start to loop data
for([])
{
// create HTML element to represent that data
$('<div></div>')
.data([])
.addClass([])
.on([])
.insertAfter(entry_set);
}
// modify DOM only once all the entries are consolidated to a single jQuery object
entry_set.appendTo('.entries');
The comments say it all. In short - the idea is to modify document DOM only once when inserting data. I would usually go HTML string approach (simply concatenating the same structure using a string), but I am interested whether anything similar to this might work as well.
You could create an empty DOM element and .append() to that
var entry_set = $("<div>"); //empty dom element
// start to loop data
var i = 4;
while(i--) {
// create HTML element to represent that data
var item = $('<div>', {
text: "test " + i
});
entry_set.append(item);
}
// modify DOM only once all the entries are consolidated to a single jQuery object
$("body").append(entry_set.children());
working demo at: http://jsfiddle.net/F2J6g/1/
EDIT
You can also start with an empty collection and use .add()
var entry_set = $(); //empty collection
// start to loop data
var i = 4;
while(i--) {
// create HTML element to represent that data
var item = $('<div>', {
text: "test " + i
});
entry_set = entry_set.add(item);
}
// modify DOM only once all the entries are consolidated to a single jQuery object
$("body").append(entry_set);
http://jsfiddle.net/F2J6g/2/
#Guy, I don't think you will get the desired HTML output. try using "wrap".
Sample Syntax:
$('.OuterDiv').wrap('<div class="abc" />');
Unfortunately, because those objects aren't actually attached to any heirarchy, calling insertAfter doesn't actually mean anything. What you'll need to do is put them inside a containing div, maybe something like this:
var entry_set = $('<div>');
// start to loop data
for([])
{
// create HTML element to represent that data
$('<div></div>')
.data([])
.addClass([])
.on([])
.appendTo(entry_set);
}
// modify DOM only once all the entries are consolidated to a single jQuery object
entry_set.appendTo('.entries');
I haven't tested that, but I think it should work.
I am using sessions to populate a multi select box with options in my Zend application.
The user selects one or more options and fills in other fields on the form and then submits. If the user didn't select all of the options in the multi select then the form is displayed again but the multi select only has the options that the user did not select the last time. This process goes on until there are no more options from the multi select left to process.
Here is the code I use to get rid of the options that have already been processed so that they are not used to populate the multi select box:
if($form_successful){
// TODO remove $post['keyword_names'] (i.e. already processed) from $keyword_names (that come from $_SESSION)
$keyword_names = array_diff($keyword_names, $post['keyword_names']);
print_r($keyword_names);
if(is_array($keyword_names) && !empty($keyword_names)){
// save updated $keyword_names into $_SESSION['workflow1']
$session = new Zend_Session_Namespace('workflow1');
$session->keyword_names = $keyword_names;
// set flag to false so that we display form again
$form_successful = false;
}else{ // all keywords have been assigned
// go to next step
$this->_redirect('/workflow-1/step-'.($step+1).'/');
}
}
print_r($keyword_names); displays the correct options, however when the form is loaded when the user submits, the multi select displays the options that were there from the begining ie the options the user has just selected and submitted are not being taken out of the multi select, it is only when the user submits the form again then the multi select box updates.
Appreciate the help.
Solved the issue by making use of URL parameters. Here is the code (might differ a lot from what I posted first because some big changes were made):
// after successful form submission
if($form_successful){
// remove $post['keyword_names'] (i.e. already processed) from $keyword_names (that come from $_SESSION)
$keyword_names = array_diff($keyword_names, $post['keyword_names']);
// save remaining $keyword_names into $_SESSION['workflow1']
$session = new Zend_Session_Namespace('workflow1');
$session->keyword_names = $keyword_names;
if(is_array($keyword_names) && !empty($keyword_names)){
// redirect to the same step again - to ensure that the form will reflect (in select lists) newly created AdGroup and/or Campaign
// GET parameteres ($params_array) provide a way to remember user's choice
$params_array = array();
if(!empty($post['match_type_id'])){
$params_array['match_type_id'] = $post['match_type_id'];
}
if(!empty($post['with_permutations'])){
$params_array['with_permutations'] = $post['with_permutations'];
}
if(!empty($ad_group_id)){
$params_array['ad_group_id'] = $ad_group_id;
}
$this_step_url = UrlUtils::assemble('', $this->getRequest()->getActionName(), $this->getRequest()->getControllerName(), $this->getRequest()->getModuleName(), $params_array);
$this->_redirect($this_step_url);
}else{ // all keywords have been assigned
// go to next step
$this->_redirect('/workflow-1/step-'.($step+1).'/');
}
}
So you don't have any code about Zend_Form object here. How do you populate the form element? If you post your class code which extends Zend_Form (or any other code dials with your form) then I may help. But in any case you can populate your multiselectbox with setMultiOptions() method or addMultiOption() for each item in multiselectbox.