I have the following
var invest401_2 = $("input[type=hidden][name=invest401_2]").val();
var invest401_3 = $("input[type=hidden][name=invest401_3]").val();
var invest401_4 = $("input[type=hidden][name=invest401_4]").val();
var invest401_5 = $("input[type=hidden][name=invest401_5]").val();
var invest401_0label = Math.round(((invest401_0/balance401)* percent401kb));
var invest401_1label = Math.round(((invest401_1/balance401)* percent401kb));
var invest401_2label = Math.round(((invest401_2/balance401)* percent401kb));
var invest401_3label = Math.round(((invest401_3/balance401)* percent401kb));
var invest401_4label = Math.round(((invest401_4/balance401)* percent401kb));
var invest401_5label = Math.round(((invest401_5/balance401)* percent401kb));
$("#invest401_0").text(invest401_0label+'%');
$("#invest401_1").text(invest401_1label+'%');
$("#invest401_2").text(invest401_2label+'%');
$("#invest401_3").text(invest401_3label+'%');
$("#invest401_4").text(invest401_4label+'%');
$("#invest401_5").text(invest401_5label+'%');
Having the count total - ex. 5
How do a throw this into a for each loop.
I tried but didnt work.
Try this
var invest401_label = [];
var invest401 = [];
for(i=0;i<6;i++)
{
invest401[i] = var invest401_2 = $("input[type=hidden][name=invest401_"+i+"]").val();
invest401_label[i] = Math.round(((invest401[i]/balance401)* percent401kb));
$("#invest401_"+i).text(invest401_label[i]+'%');
}
Take a look at $.each.
http://api.jquery.com/jQuery.each/
If you add a class to this elements $("input[type=hidden][name=invest401_2]").val(); you can get them as an array and use each.
If you add a class named elements. Use the following example.
$('.elements').each(function(i, element) {
var invest = $(element).val();
$(element).val(Math.round((invest/balance401)* percent401kb));
});
Or
var $elements = $('.elements');
for(var i in $elements) {
var element = $elements[i];
element.val(Math.round((element.val()/balance401)* percent401kb));
}
Assuming there is only #invest401_0 - 5
$("[id=^invest401_]").each(function(){
$(this).text(Math.round((($("input[type=hidden][name="+this.id+"]").val()/balance401)* percent401kb))+'%');
});
Reference
http://api.jquery.com/jQuery.each/
http://api.jquery.com/category/selectors/
Although this may not work ( I'm currently writing this from an ie8 machine) this /should/ do what you want correctly and replaces all of the code you have there
for (i = 0; i < $('.hiddenelems').size(); i++) {
$('.hiddenelems:eq('+i+')').text(Math.round((($('.hiddenelems:eq('+i+')').val()/balance401)* percent401kb))+'%');
}
Related
I want to randomly apply styles to my ArtLayers.
To do this, I tested the applyStyle function.
ArtLayers.applyStyle
It works!)
But to solve my task I need all styles.
I can't find it in the documentation (photoshop-javascript-ref-2020).
Can you suggest something?
To get all the styles use this Action Manager function:
var allStyles = get_styles()
alert(allStyles); // ALLL the styles!
//alert(allStyles[22]); // Sunspots (texture)
function get_styles()
{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var appDesc = executeActionGet(ref);
var List = appDesc.getList(stringIDToTypeID('presetManager'));
var list = List.getObjectValue(3).getList(charIDToTypeID('Nm '));
var styleNames=[];
for (var i = 0; i < list.count; i++)
{
var str = list.getString(i);
styleNames.push(str);
}
return styleNames;
}
This code below is a javascript code to change texts and background properties from the body tag at the same time when the browser is clicked. I've just followed rules in w3school.com, actually, I'm doing the same as the examples do, but mine won't work and I've failed to find my fault. plz, help me.
var helloDiv = document.createElement("div");
var helloText = document.createTextNode("hi.");
helloDiv.appendChild(helloText);
var bodyntext = document.getElementsByTagName("body").appendChild(helloDiv);
var complementary = new Array();
var j = 0;
window.onclick = function(){
var background_color;
var body = document.getElementsByTagName("body")[0];
// var color = new Array();
var result = null;
var number = Math.round(Math.random()*0xFFFFFF);
for(var i = 0; i > 3; i++)
{
complementary[i] = (255-(number.slice(j,j+1).toString(10))).toString(16);
j = j + 2;
}
var clnumber = (complementary[0]+complementary[1]+complementary[2]).toString(16);
body.style.backgroundColor = "#"+ number.toString();
bodyntext.style.color = "#"+ clnumber.toString();
}
Here's your problem
var bodyntext = document.getElementsByTagName("body").appendChild(helloDiv);
Please note that the javascript functions that have plural names often return arrays which is the case here
document.getElementsByTagName("body") returns an array and you are trying to perform an invalid action, that is append child, to this array. You need to access an element using index and then perform this action.
so using
var bodyntext = document.getElementsByTagName("body")[0].appendChild(helloDiv); should fix your problem.
Use document.getElementsByTagName("body")[0] to achieve expected result, as getElementsByTagName() method returns a collection of all elements in the document with the specified tag name, as a NodeList object.
var helloDiv = document.createElement("div");
var helloText = document.createTextNode("hi.");
helloDiv.appendChild(helloText);
var bodyntext = document.getElementsByTagName("body")[0].appendChild(helloDiv);
var complementary = new Array();
var j = 0;
window.onclick = function(){
var background_color;
var body = document.getElementsByTagName("body")[0];
// var color = new Array();
var result = null;
var number = Math.round(Math.random()*0xFFFFFF);
for(var i = 0; i > 3; i++)
{
complementary[i] = (255-(number.slice(j,j+1).toString(10))).toString(16);
j = j + 2;
}
var clnumber = (complementary[0]+complementary[1]+complementary[2]).toString(16);
body.style.backgroundColor = "#"+ number.toString();
bodyntext.style.color = "#"+ clnumber.toString();
}
codepen - https://codepen.io/nagasai/pen/NLBXJE
Please refer this link for more details - https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName
I just change appendChild to document.body.appendChild(helloDiv) and added ID attribute for that div:
var helloDiv = document.createElement("div");
helloDiv.id = "myDIV";
document.body.appendChild(helloDiv);
var helloText = document.createTextNode("Click me");
helloDiv.appendChild(helloText);
var complementary = new Array();
var j = 0;
window.onclick = function(){
var background_color;
var body = document.getElementsByTagName("body")[0];
var helloText = document.getElementById("myDIV");
// var color = new Array();
var result = null;
var number = Math.round(Math.random()*0xFFFFFF);
for(var i = 0; i > 3; i++)
{
complementary[i] = (255-(number.slice(j,j+1).toString(10))).toString(16);
j = j + 2;
}
var clnumber = (complementary[6]+complementary[1]+complementary[2]).toString(16);
var clnumber2 = (complementary[0]+complementary[2]+complementary[6]).toString(11);
body.style.backgroundColor = "#"+ number.toString();
helloText.style.color = "#"+number.toString();
}
I'm doing practical Javascript course on watchandcode.com and my code is exactly the same as the instructor's, but the part where it clears the inputs does not work here.
changeTodo: function () {
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput').valueAsNumber;
var changeTodoTextInput = document.getElementById('changeTodoTextInput').value;
todoList.changeTodo(changeTodoPositionInput, changeTodoTextInput);
// THIS PART DOESN'T WORK
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
}
The function does its job, it's just those last two lines that don't work.
You're trying to clear a value and not input elements, you have to get the DOM elements first :
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');
Then get the value after that from them :
todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
And now you could clear the value of the both inputs using :
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
FULL CODE :
changeTodo: function() {
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');
todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
}
Hope this helps.
You can try this:
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput').value = "";
var changeTodoTextInput = document.getElementById('changeTodoTextInput').value = "";
I have these following arrays
var category = ['Guitar', 'Bass', 'Amps'];
var platform_a = ['platform-a1','platform-a2','platform-a3'];
var platform_b = ['platform-b1','platform-b2','platform-b3'];
var platform_c = ['platform-c1','platform-c2','platform-c3'];
And I want to convert them into a json which should look like this
{
"Guitar":["platform-a1","platform-a2","platform-a3"],
"Bass":["platform-b1","platform-b2","platform-b3"],
"Amp":["platform-c1","platform-c2","platform-c3"]
}
How would I do this? I would have to do this in pure javascript
Let's present three different approaches to your case:
First one
If you want just to create the json object with your data try:
http://jsfiddle.net/csdtesting/tap2xom9/
var platform_a = ['platform-a1', 'platform-a2', 'platform-a3'];
var platform_b = ['platform-b1', 'platform-b2', 'platform-b3'];
var platform_c = ['platform-c1', 'platform-c2', 'platform-c3'];
var category = ['Guitar', 'Bass', 'Amps'];
var obj = {
Guitar: platform_a,
Bass: platform_b,
Amps: platform_c
};
document.write(JSON.stringify(obj));
Second
If you want to create it dynamically do something like that:
http://jsfiddle.net/csdtesting/tap2xom9/
var category = ['Guitar', 'Bass', 'Amps'];
var platform_a = ['platform-a1', 'platform-a2', 'platform-a3'];
var platform_b = ['platform-b1', 'platform-b2', 'platform-b3'];
var platform_c = ['platform-c1', 'platform-c2', 'platform-c3'];
var FinalObject = {};
FinalObject[category[0]] = platform_a;
FinalObject[category[1]] = platform_b;
FinalObject[category[2]] = platform_c;
document.write(JSON.stringify(FinalObject));
Finally
If you want to be more dynamic then try this :
http://jsfiddle.net/csdtesting/kqwz72os/
var FinalObject = {};
var category = ['Guitar', 'Bass', 'Amps'];
var platforms = {
platform_a: ['platform-a1', 'platform-a2', 'platform-a3'],
platform_b: ['platform-b1', 'platform-b2', 'platform-b3'],
platform_c: ['platform-c1', 'platform-c2', 'platform-c3']
};
for (var i = 0; i < category.length; i++) {
FinalObject[category[i]] = platforms[Object.keys(platforms)[i]];
}
document.write(JSON.stringify(FinalObject));
Hope this helps!
There's no reasonable shortcut here. You just have to do it manually:
var jsonString = JSON.stringify({
"Guitar": platform_a,
"Base": platform_b,
"Amp": platform_c
});
I generate an html table using YUI/JavaScript. I am now trying to go through and store the information that is generated by looking at each item in the table using a for loop.
var savedata = function(){
var tabledata = Y.one("#generatedtable")._node;
var jData = [];
for (var r in tabledata.rows){
var samplerow = {};
samplerow.timestamp = r.cells[0].innerHTML;
samplerow.volts = r.cells[1].innerHTML;
samplerow.amps = r.cells[2].innerHTML;
samplerow.kW = r.cells[3].innerHTML;
samplerow.kWh = r.cells[4].innerHTML;
samplerow.sessionid = r.cells[5].innerHTML;
jData.push(samplerow);
}
};
using Chrome developer tools I can see that Y.one("#generatedtable")._node.rows gives me an HTMLcollection of [X]. This signifies the number of rows that were created by my previous function. Row[0] is just headers and I want to skip over this row. The loop fails on the
samplerow.timestamp = r.cells[0].innerHTML;
line. How should I structure this for loop to go through rows[0] to [X] to store in my JSON Data variable?
var savedata = function(){
var tabledata = Y.one("#generatedtable")._node;
var jData = [];
var i = 1;
var length = tabledata.rows.length
while (i<length){
var samplerow = {};
var r = tabledata.rows[i];
samplerow.timestamp = r.cells[0].innerHTML;
samplerow.volts = r.cells[1].innerHTML;
samplerow.amps = r.cells[2].innerHTML;
samplerow.kW = r.cells[3].innerHTML;
samplerow.kWh = r.cells[4].innerHTML;
jData.push(samplerow);
i++;
}
alert(jData);
};