First time asking a question, let me know if I'm doing this wrong. I have an AppleScript that takes values from a Numbers document, and enters them into various fields of a webform using document.getElementById.
It works flawlessly, but now I want to add a feature so that it will only fill in a value if that field in the webform is blank.
My idea for the code was going to be something like this:
if ("document.getElementById('something').value = null)
execute javascript ("document.getElementById('something').value = '" & valueToFillIn & "'")
else
move on
Can someone advise me on the best way to check if a document.getElementById value is null, and then how to proceed? Very much appreciated!
Heres a very simple alternative as well. Modify for your needs. I believe its well explained and obvious of what is going on in the script. The else statement is not required to "move on". Once the if statement is checked, the script moves on without needing the else unless you want to execute something else if that field has value.
-- Tested Here -> http://meyerweb.com/eric/tools/dencoder/
set fieldValue to getInputById("dencoder") of me
if fieldValue is "" then
inputByID("dencoder", "Im Allowed to Input Text Because You Were Empty") of me
else
say "The Field Was Not Empty"
end if
-- Simple Handlers
-- Retrieve Value of elementById
to getInputById(theId)
tell application "Safari"
set output to do JavaScript "document.getElementById('" & theId & "').value;" in document 1
end tell
return output
end getInputById
-- Input My String to element by Id
to inputByID(theId, theValue)
tell application "Safari"
do JavaScript " document.getElementById('" & theId & "').value ='" & theValue & "';" in document 1
end tell
end inputByID
A few things you have to consider before use this snippet:
1- When this code will be triggered? Regularly in the click eventListener, like the example below:
2- Which value do you want to post? Please be more specific.
document.getElementById("myButton").addEventListener("click", checkInputs);
function checkInputs() {
if (document.getElementById("01").value == ""){
var newValue = document.getElementById("02").value;
document.getElementById("demo").innerHTML=newValue;
}
}
https://jsfiddle.net/azwt542p/2/
There are a few methods to get input textbox value straight (without wrapping the input element inside a form element):
These methods returns a collection of elements, so use [whole_number]
to get the desired occurrence, for first element use [0] and for
second one use 1 and so on...
Alternative 1:
Use document.getElementsByClassName('class_name')[whole_number].value
which returns a Live HTMLCollection
Eg. document.getElementsByClassName("searchField")[0].value;if this is
the first textbox in your page.
Alternative 2:
Use document.getElementsByTagName('tag_name')[whole_number].value
which also returns a live HTMLCollection
Eg. document.getElementsByTagName("input")[0].value; ,if this is the
first textbox in your page.
Alternative 3:
Use powerful document.querySelector('selector').value which uses CSS
selector to select element
Eg. document.querySelector('#searchTxt').value; selected by id
document.querySelector('.searchField').value; selected by class
document.querySelector('input').value; selected by tagname
document.querySelector('[name="searchTxt"]').value; selected by name
Related
I have two custom context menu (one was written by my, the other one was already there). Mine show up only when a word is underlined because he is poorly written and the other one show up on table so the user can choose to add another line or to delete a line from this table.
The trouble I got is that the one working on table is blocking mine to execute if there is an error in the table and you want to correct it.
For this contextMenu, they write it so they got a range object from the place where the right click is done.
document.oncontextmenu = elementSelect;
function elementSelect(){
Rng = document.selection.createRange();
}
I've tried to get the className of the tag inside the range object so if it match the class from my error tag (a span with class="error"), I do a simple return in the contextMenu for the table so it does not trigger.
var rngClassName = Rng(0).getAttribute("class").value;
alert("class =>" + rngClassName);
if(rngClassName == "error")
{
return;
}
The Rng(0) is used after several time to retrieve somme attribute and work fine for them. But It seems that it does not work to get the class value, no value is return, the script stop execution at line var rngClassName = Rng(0).getAttribute("class").value; and no error is returned from the debugging (F12 in IE).
It is my first time working with Range object so I think I am missing something but don't know what.
Spec : IE5, Vanilla Javascript.
Try using element.classList .
with following methods : classList.add() , classList.remove() , classList.contains()
edit: i see your comment now, and below won't work in IE5...
But if I understand your question correctly, you want to check the class of an element whitin the selection? I don't have IE5 so I checked it in 11 and Chrome. You could try something like this:
var selection = document.getSelection();
var selRange = selection.getRangeAt(0);
var parentNode = selRange.startContainer.parentNode;
var hasError = parentNode.classList.contains('error');
Demo: https://jsfiddle.net/spdwn7bo/1/ (select a text within 2 seconds :))
When writing a new email, I've got a modal(pop-up window in boostrap) that shows a list of contacts. When I select (through checkboxes) a couple of contacts, the selected ones are written into a checkbox. Problem is I'm just writing the lastone I select instead of all of the selected ones.
If you need further explanation please ask. (Sorry for my english)
$("#tblContacto").on("click", ".ck", function(event){
if($(".ck").is(':checked')) {
selected_index = parseInt($(this).attr("alt").replace("Check", ""));
var contacto = JSON.parse(tbContactos[selected_index]);
$("#txtDestinatarios").val(contacto.Email);
} else {
$("#txtDestinatarios").val("");
}
});
Assuming that you want to add all E-Mails into a textfield with id txtDestinatariosthe cause of your Problem is the usage of the $("#txtDestinatarios").val(); function.
Calling val() with an argument sets (and thus overwrites) the value within the textfield. (See demo at http://api.jquery.com/val/#val2)
You would have to first retrieve the value of the textfield using code like var currentValue = $("#txtDestinatarios").val() and then add/remove the E-Mail from/to the string before setting the resulting string back as the value.
If you want to set all selected items in the checkboxes into Textfiled you can use the following line of code :-
$("#txtDestinatarios").val( $("#txtDestinatarios").val()+ ","+contacto.Email);
I don't know much about JavaScript but feel that this should be fairly easy. I have a drop down list with several options and the below is under the 'Click' option:
form1.CapExForm.Subform1.Table1.HeaderRow[0].DropDownList1::click - (JavaScript, client)
I would like to type in someone's name populate in a text field that has the header scrip below:
form1.Subform3.ApprovalSubForm.Table3.Row2.RequestorName::click - (JavaScript, client)
I had previously done this in a PDF form but needed the form to flow and add rows so moved to LiveCycle. Below is the script ("Software >$1,000,000" was from the dropdown list and all information below that would be the textfields I'm wanting to populate based off that selection) I had in Adobe PDF form if that helps clarify what I am attempting:
if (event.value=="Software >$1,000,000") {
this.getField("Name").value = "Type Requestor Name";
this.getField("Name_2").value = "Type Dept. Manager";
this.getField("Name_3").value = "Todd Stephenson";
this.getField("Name_4").value = "John Kapchinske";
this.getField("Name_5").value = "N/A";
this.getField("Text2").value = "Approval Reviewed: Hazel Welch";
this.getField("Signature").value = "Must click, sign, and save to add signature";
this.getField("Signature_2").value = "Must click, sign, and save to add signature";
this.getField("Signature_3").value = "Must click, sign, and save to add signature";
this.getField("Signature_4").value = "Must click, sign, and save to add signature";
this.getField("Signature_5").value = "Must click, sign, and save to add signature";
this.getField("Signature1").value = "Must click, sign, and save to add signature";
}
Ok - I'm not sure I understand what you want to do, but I'm giving it my best guess. You want some other fields to populate when you select a value from the dropdown, correct?
The javascript for Designer works differently than that for Acrobat. Here is a good reference for that:
Livecycle Scripting Reference.
And here is a more comprehensive list of the properties available through javascript in Designer.
First, put it in the change event for the dropdown. If you put it in the click event it'll run whenever they click on the field, even if they don't make a change.
In that event, put javascript like this:
if (xfa.event.newText == "Software >$1,000,000")
{
field1SOMexpression.rawValue = "Value you want";
field2SOMexpression.rawValue = "Next value you want";
//add the other fields you want to reference here
}
Here, xfa.event.newText refers to the value of the dropdown after the change. The property .newText is only available during a change event, and you want to use that one because during the change event, the rawValue of that field is whatever the original value was. The rest of the time you would want to use .rawValue to access the value of a field, and that's why we are using it to set the value of the other fields (because they aren't the fields that triggered the change event.)
A SOM expression is a reference to a field. Example - based on what you pasted above, the SOM expression for the dropdown is form1.CapExForm.Subform1.Table1.HeaderRow[0].DropDownList1.
Without seeing the form I can't tell you what the expression for the fields you want to set values would be, but there are a couple of ways to get the field expression for each field you want to set. The simplest way is to go to the script editor, hold the control and shift keys, and click on the field; this inserts its SOM expression into the script window.
EDIT: If you want the value to be based on the combo of two fields, then it might be easier to use a calculate script in the field that needs to change.
Put script into the calculate event for the field whose value you want to set, to decide what its value should be based on the values of the two dropdowns.
It should be something like this:
var myValue = "";
var type = form1.CapExForm.Subform1.Table1.HeaderRow[0].DropDownList1.rawValue;
var price = ....(whatever the som expression is).PriceRange.rawValue;
if (type == "Software" && price == "1,000,000")
{
myValue = "Value you want";
}
else if (type == "Something else" && price == "Some other price . .")
{
myValue = "Some other value");
}
this.rawValue = myValue;
In that case, 'this' would refer to the field that you want to change the value on, since the script would be running in that field's calculate event. The calculate event fires whenever something changes in one of the fields that are referenced in the calculate script.
I got 6 "textboxex" and an Array with them.
<input id="slot0" type="text" /> id from 0 to 5, also Array named "slotarray". I want arrray and textboxes to be bound slotarray[0] with input id="slot0" etc.
First i needed function that will find first empty field in array (no matter if corresponding textbox is empty - but should) and put there string (short string - shortcode like "abc" or "sp1").
This function also need to populate bound textbox with long string.
If slotarray[2] == 'abc' then with the same number in ID (here be id="slot2") need to contain long string like "Abrasive Brilliant Conexant".
Here what i got
click to populate
and then function
function populate(shortstring,longstring) {
for (var i=0; i<6; i++) {
if (slotarray[i] == '') {
slotarray[i] = shortsrting;
slotid = 'slot' + i;
document.getElementById(slotid).value = longstring;
break;
}
}
}
With clearing at the moment of creating: ( Array('','','','','','') ), and textbox .value=''; its working as it should.
But then i figured out that i need function to clear textbox and bound array field. Not all but one specific for one clic. So instead of 6 functions i start to wrote
clear this field
for each of textbox, with different numbers and ids ofcourse, and clearing function:
function clear(arrayid, slotid) {
slotarray[arrayid] = '';
document.getElementById(slotid).value = '';
}
But this function do not clearing textbox neither array. I see that textbox has text, and i know that array isn't cleared because first function works finding first empty object...
What am i doing wrong here? its definition of "empty"/"cleared" filed/textbox? maybe i need to use more complex conditions? maybe it is something else.
Maybe i don't need array (i can manage to get rid of short-codes) and just make functions work only on textboxes?
Ok - i prepared jsfiddle demo with this, but even populating don't work..
http://jsfiddle.net/BYt49/11/
You can't use the keyword clear because refers to the (deprecated) function document.clear; so try to change the name of your "clear" function.
Ok, whatever you have written is fine. Just change to way you call your javascript.
Here is jsfiddle: http://jsfiddle.net/BYt49/20/
I have created dynamic select box using jquery. I have created the select box as an array name=Child[]. See my code
for(i=1;i<=val;i++){
var newParagraph = $('<dl class="thirty fl"><dt>Child '+i+'</dt> <dd><select name="child[]"><option value="">--select--</option><option value="1">1</option><option value="2">2</option><option value="3">3</option</select></dd></dl>');
$('#childDynamic').append(newParagraph);
}
In form validation section (in javascript), how I validate this select box? I don't know how to get the values from an array of select box using JavaScript
Anyone can help me?
Thanks in advance
(You don't actually have an array of select elements, given that html doesn't have arrays. But anyway...)
If you use the name attribute as the selector you can get a jQuery object containing the select elements:
$('#childDynamic select[name="child\\[\\]"]')
...which you can then process as you see fit.
Because square brackets have special meaning in jQuery selectors they need to be escaped with backslashes, and to include backslashes in a string literal they need to be escaped too.
Anyway, I don't know what kind of validation you want to apply, but if you wanted to loop through each select and check its value in some way you could do something like this:
var valid = true;
$('#childDynamic select[name="child\\[\\]"]').each(function() {
var val = $(this).val();
// example validation: make sure something other than the default
// blank value is selected
if (val === "") {
valid = false;
}
});
if (!valid) {
// do something
}