get information from array to input - javascript

how i get information from array and send it to input?
i have a filter, after filtering information i have 2 value (v1,v2) and 2 button - for v1 and v2. I need to do- by pressing the button v1 information from mynewvarv1 go to input whith id='one' - can you help me to write this function?
sorry for my english
var myvar = [ // filter start
{name:'v1',val1:'31',val2:'',val3:true,val4:false,val5:false,val6:false,},
{name:"Срочный на 91 день",val1:'91',val2:'',val3:true,val4:false,val5:false,val6:false,},
{name:"v2",val1:'181',val2:'',val3:true,val4:false,val5:false,val6:false,},
];
document.getElementById('filterButton').onclick = function(){
var filter = [];
filter.push(document.getElementById('val1').options[document.getElementById('val1').selectedIndex].value);
filter.push(document.getElementById('val2').value);
filter.push(rbvalue('val3')=='1');
filter.push(rbvalue('val4')=='1');
filter.push(rbvalue('val5')=='1');
var filtered_data = [];
var data_accepted = true;
for(i=0;i<myvar.length;i++){
data_accepted = true;
for(j=0;j<filter.length;j++){
if(filter[j]!=='' && filter[j]!=myvar[i][('val'+(j+1))]){
data_accepted = false;
}
}
if(data_accepted){
filtered_data.push(myvar[i]);
}
}
function getmesseg (){
container.innerHTML = ' v1 <button onclick="myFunction()">Try it</button>'
container.innerHTML = ' v2<button onclick="myFunction()">Try it</button>'
var mynewvarv1 = [ {vaal1:value="12",vaal2:'10.2',vaal3:'9',} ];
var mynewvarv2 = [ {vaal1:value="12",vaal2:'10.2',vaal3:'9',} ];
//what the function this needs???
};
function ab(){ if(filtered_data[i].name == "v1","v2") {getmesseg(); }};
var container = document.getElementById('container');
container.innerHTML = '';
for(i=0;i<filtered_data.length;i++){
container.innerHTML+=filtered_data[i].name+'<br />';
ab();
}
};
function rbvalue(html_name){
var undefined,default_value,i,rb_collection = document.getElementsByName(html_name);
if(rb_collection!==undefined){
default_value = rb_collection[0].value;
}
for(i=0;i<rb_collection.length;i++){
if(rb_collection[i].checked){
return rb_collection[i].value;
}
}
return default_value;
}// filter end

You should be able to use:
var input1 = document.getElementById("one");
input1.value = mynewvarv1[0].vaal1;

Related

how can I manipulate ScriptUI state in Illustrator based on document state instead of button click?

Trying to hide a button depending on current document name. If the document is open then the button is hidden else display button.
The current code is not displaying the button at all.
if(!app.documents.length){
add_group_Ai();
}
else{
var currentDocument = app.activeDocument.name;
var str = aiProjectFile.fullName;
var fileName = str.substr(str.lastIndexOf("/")+1,str.length);
if(fileName == currentDocument){
alert('document already open');
remove_group_Ai();
}
}
function remove_group_Ai(){
group_Ai.remove(this.parent);
dialog.layout.layout(true);
}
function add_group_Ai(){
// GROUP_AI
// ========
var group_Ai = panel_namePlate.add("group", undefined, {name: "group_Ai"});
group_Ai.orientation = "column";
group_Ai.alignChildren = ["fill","top"];
group_Ai.spacing = 10;
group_Ai.margins = 5;
group_Ai.alignment = ["center","top"];
var btn_AiProj = group_Ai.add("button", undefined, undefined, {name: "btn_AiProj"});
btn_AiProj.text = "Open Illustrator Project";
btn_AiProj.alignment = ["center","top"];
btn_AiProj.onClick = function(){
aiProjectFile = File.openDialog();
if (aiProjectFile) {
var str = aiProjectFile.fullName;
var fileName = str.substr(str.lastIndexOf("/")+1,str.length);
projPathText.text = fileName;
app.open(aiProjectFile);
}
};

how do i sum all array arguments

I want to have a webpage with one input box and two button. first button for adding input box value to my array and the second to sum all array values. but it return 0 :(
<input type="text" placeholder="Enter number" id="input">
<button id="add">Add to Array</button>
<button id="sum">Sum All</button>
<p id="text"></p>
<script>
var myArr = []; //contain numbers that i've inputed
var input = document.getElementById('input');
var addBtn = document.getElementById('add');
var sumBtn = document.getElementById('sum');
addBtn.onclick = function () {
myArr.push(input.value);
input.value = "";
}
function sumAll() {
var i , sum = 0;
for ( i in myArr.length ) {
sum += i;
}
return sum;
}
sumBtn.onclick = function(){
var paragraph = document.getElementById('text');
var a = sumAll();
paragraph.innerHTML = a;
}
</script>
You can use the for-of syntax to loop thru your array.
function sumAll() {
var i , sum = 0;
for ( i of myArr ) {
sum += Number(i);
}
return sum;
}
You need to run your for loop properly:
When you say for ( i in myArr.length ) {the value of i comes out as undefined so the summation never happens. You should use debugging to figure out such issues.
function sumAll() {
var i, sum = 0;
for (i = 0; i < myArr.length;i++) {
sum += +myArr[i];
}
return sum;
}
Notice +myArr[i];that uses unary operator +. This can attempt to convert the string in the input field to number(both int and floats).
https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Expressions_and_Operators#Arithmetic_operators
This should be way to go..
1.You have to use parseInt to treat values as ints
2.Loop correctly over array
<script>
var myArr = []; //contain numbers that i've inputed
var input = document.getElementById('input');
var addBtn = document.getElementById('add');
var sumBtn = document.getElementById('sum');
addBtn.onclick = function () {
myArr.push(input.value);
input.value = "";
}
function sumAll() {
var i , sum = 0;
for ( i in myArr ) {
sum += parseInt(myArr[i]);
}
return sum;
}
sumBtn.onclick = function(){
var paragraph = document.getElementById('text');
var a = sumAll();
paragraph.innerHTML = a;
}
</script>

Javascript: window.confirmation

I have a function:
function placeOrder(price, productList) {
var bulletinBoardItem = Number(productList.box1.value);
var stickersItem = Number(productList.box2.value);
var cutoutsItem = Number(productList.box3.value);
var trimmerItem = Number(productList.box4.value);
var resourceBooksItem = Number(productList.box5.value);
var price = new Array(5);
price[0] = 12;
price[1] = 1;
price[2] = 6;
price[3] = 3;
price[4] = 20;
var sumBB = bulletinBoardItem * price[0];
var sumStickers = stickersItem * price[1];
var sumCutouts = cutoutsItem * price[2];
var sumTrimmer = trimmerItem * price[3];
var sumRB = resourceBooksItem * price[4];
}
and I have a window.confirm box that I need to reference the above function from, but none of the variables are recognized in that function. I don't understand what I'm doing wrong here.
<input type="button" onClick="placeOrder()" value="Place Order">
<p id = "order"></p>
<script>
function placeOrder(quantity, price, productList) {
var r = confirm("You've ordered Bulletin Boards");
if (r) {
window.alert("Your order has been placed!!!")
} else {
window.alert("Your order has been canceled.");
}
document.getElementById("order").innerHTML = txt;
}
</script>
You are using txt which is not defined :
document.getElementById("order").innerHTML = txt;
Thus, try to change txt by any defined string and your script will not be failed

How to output a reverse string using innerHTML?

I'm trying to reverse a string and output the results using innerHTML. Nothing is output when I click the button.
<input type="text" id="text">
<button id="reversal">Reverse</button>
<div id="results"></div>
<script type="text/javascript">
var input1 = document.getElementById("text").value;
var button = document.getElementById("reversal");
var result = document.getElementById("results");
var reversals = function(string) {
result.innerHTML = string.split("").reverse().join("");
}
button.onlclick = function() {
reversals(input1);
}
</script>
This is what you should do.
input1 = document.getElementById("text").value; should be assigned within onclick handler.
var button = document.getElementById("reversal");
var result = document.getElementById("results");
var reversals = function(string) {
result.innerHTML = string.split("").reverse().join("");
}
button.onclick = function() {
var input1 = document.getElementById("text").value;
reversals(input1);
}
As already mentioned you have a typo, you need to change button.onlclick to button.onclick. Next when you do a refersal it still has the value of text from when the document loaded, which is empty. You need to get the new value:
button.onclick = function() {
var input1 = document.getElementById("text").value;
reversals(input1);
}

Not working with Edit Button in Javascript

I'm Creating an Edit button that will edit my data in my row. the problem is if I click multiple edit button it doesn't comply.
It only complies when I click 1 edit button.
HERE IS MY EXAMPLE : http://jsfiddle.net/te2wF/31/
HERES MY CODE
function editRow(t)
{
var i = t.parentNode.parentNode.rowIndex;
var table = document.getElementById('myTable');
if ( table.rows[i].cells[3].childNodes[0].value =="Edit")
{
var name = table.rows[i].cells[0].innerHTML;
var age = table.rows[i].cells[1].innerHTML;
var gender = table.rows[i].cells[2].innerHTML;
var tname = table.rows[i].cells[0];
var tage = table.rows[i].cells[1];
var tgender = table.rows[i].cells[2];
tname.innerHTML = "";
var textname = document.createElement("input");
textname.id = "textname";
tname.appendChild(textname);
tage.innerHTML = "";
var textage = document.createElement("input");
textage.id = "textage";
tage.appendChild(textage);
tgender.innerHTML = "";
var textgender = document.createElement("select");
textgender.id = "textgender";
tgender.appendChild(textgender);
document.getElementById('textname').focus();
document.getElementById("uid").innerHTML = i;
document.getElementById("textname").value = name;
document.getElementById("textage").value = age;
textgender.options.add(Option(gender));
if(gender == "Male") textgender.options.add(new Option("Female"));
else textgender.options.add(new Option("Male"));
table.rows[i].cells[3].childNodes[0].value="Save"
document.getElementById("name").disabled = true;
document.getElementById("age").disabled = true;
document.getElementById("gender").disabled = true;
document.getElementById("insert").disabled = true;
}
else
{
update(document.getElementById('uid').innerHTML);
table.rows[i].cells[3].childNodes[0].value="Edit"
document.getElementById("insert").disabled = false;
}
}
Thank you for helping.....
you should change:
textgender.options.add(Option(gender));
to
textgender.options.add(new Option(gender));
But more to the point on the edit button:
You're attempting to access the editable inputs on your table by id - but when you create a second set of textname and textage inputs, the id's are the same as the other row that you're editing. You either need to create unique id's on these elements (perhaps by appending the row number to the id), or you need to set it up so that you can only edit one row at a time (disable and grey the other rows when you're editing one)
Please, when using jsfiddle, please keep javascript on javascript tab and html on html tab to make it easier and faster to help you.
I believe you should adjust 2 places on your code:
First
document.getElementById('textname').focus();
document.getElementById("uid").innerHTML = i;
document.getElementById("textname").value = name;
document.getElementById("textage").value = age;
textgender.options.add(Option(gender));
should be
document.getElementById('textname').focus();
document.getElementById("uid").innerHTML = i;
document.getElementById("textname").value = name;
document.getElementById("textage").value = age;
textgender.options.add(new Option(gender));
Second:
else
{
update(document.getElementById('uid').innerHTML);
table.rows[i].cells[3].childNodes[0].value="Edit"
document.getElementById("insert").disabled = false;
}
should be:
else
{
update(document.getElementById('uid').innerHTML);
table.rows[i].cells[3].childNodes[0].value="Edit"
document.getElementById("name").disabled = false;
document.getElementById("age").disabled = false;
document.getElementById("gender").disabled = false;
document.getElementById("insert").disabled = false;
}
try here http://jsfiddle.net/te2wF/31/

Categories

Resources