Remove <param> node from <object> and replace it with new <param> values - javascript

How can I add tag to Object , replacing its original tag with modified values. I am using the JavaScript to perform this replacement.
But the param is not getting replaced or removed.
how to identify a particular param under
Can any one suggest please.
Thank you!
function autoObjectFun() {
var objects = document.getElementsByTagName('object');
var len1 = objects.length;
for(var j = 0; j < len1; j++) {
var paramObj = objects[j].getElementsByTagName('param');
var len = paramObj.length;
for(var i = 0; i < len; i++) { var srcStr = paramObj[i].getAttribute('name'); if (srcStr == 'flashvars' ) { var newParamObj = document.createElement('param'); newParamObj = paramObj[i].cloneNode(true); var params = paramObj[i].getAttribute('value'); var newparams = ''; var paramplay = 'autoplay=0&'; newParamObj.setAttribute('value', paramplay);
paramObj[i].removeNode(true);
var newObject = objects[j].cloneNode(true);
var parent1 = objects[j].parentNode;
newObject.appendChild(newParamObj);
parent1.replaceChild(newObject,objects[j]); }
}}}

There is no native method removeNode(), this will cause an error and stop the further execution of your statements.
Try:
paramObj[i].parentNode.removeChild(paramObj[i]);
..instead.

Related

Setting multiple items in array to disabled with JavaScript

The current process of setting multiple inputs to disabled works for me, but seems to be way too much code due to the multiple for loops:
var textEditors = document.getElementsByClassName('textEditor'),
textareas = document.getElementsByTagName('textarea'),
radioInputs = document.getElementsByClassName('radioSelect'),
textInputs = document.getElementsByTagName('input');
for (var i = 0; i < textEditors.length; i++) {
textEditors[i].disabled = true;
}
for (var g = 0; g < textInputs.length; g++) {
textInputs[g].disabled = true;
}
for (var f = 0; f < textareas.length; f++) {
textareas[f].disabled = true;
}
for (var z = 0; z < radioInputs.length; z++) {
radioInputs[z].disabled = true;
}
But this above works fine for me. The below is what I would assume would work instead - put all elements into a single array, and iterate over a single array to set each to disabled. When I view the HTMLCollection via console.log it says disabled:true yet the element on the screen is not disabled. What am I missing here?
var textEditors = document.getElementsByClassName('textEditor'),
textareas = document.getElementsByTagName('textarea'),
radioInputs = document.getElementsByClassName('radioSelect'),
textInputs = document.getElementsByTagName('input');
var normalInputs = [];
normalInputs.push(textEditors);
normalInputs.push(textInputs);
normalInputs.push(radioInputs);
normalInputs.push(textareas);
for (var i = 0; i < normalInputs.length; i++) {
console.log(normalInputs[i])
normalInputs[i].disabled = true;
}
This will serve your purpose.
In your second method you're pushing the array capture from getElement into normalInputs array and than looping through that array and applying disable property on elements of inputArray which is eventually array of all the selected elements not individual element.
var textEditors = document.getElementsByClassName('textEditor'),
textareas = document.getElementsByTagName('textarea'),
radioInputs = document.getElementsByClassName('radioSelect'),
textInputs = document.getElementsByTagName('input');
var normalInputs = [];
normalInputs.push(textEditors);
normalInputs.push(textInputs);
normalInputs.push(radioInputs);
normalInputs.push(textareas);
normalInputs.forEach(e=>{
e.forEach(ele =>{
ele.disabled = true;
})
})
Without ES6
for (var i = 0; i < normalInputs.length; i++) {
for(let j=0; j< normalInputs[i].length; j++){
normalInputs[i][j].disabled = true;
}
}
A better approach is use concat
var normalInputs = [];
normalInputs = normalInputs.concat(textEditors,textInputs,radioInputs, textareas);
for(let i=0; i<normalInputs.length; i++){
normalInputs[i].disabled = true;
}
Well, give all of them same class like .inputs and get all of them with one expression:
var items = document.querySelectorAll( '.inputs' );
if it's not possible, get all of them like this:
var items = document.querySelectorAll( 'input, .textEditor, .radioSelect, textarea' );
then you can fix that with one loop for all of them:
for ( var i = 0; i < items.length; i++ )
items[ i ].disabled = true;

javascript overwriting my data

I am trying to write a short script to get some data. The idea is, instead of writing a lot of JavaScript files and I always need to hardcode the ID in the link I thought it must be possible to insert a variable into the link. Now I have the problem I only see the last value of my var as it looks like the var will be overwritten all time instead of simply adding the ID to the output. I hope you can help me.
Here is the code:
myConnector.getData = function(table, doneCallback) {
var date = new Date();
edate = date.toISOString().slice(0,10) + "-23-59-59";
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
var id_temp = ids[i];
{
$.getJSON("https://****/api/historicdata.json?id="+id_temp+"&avg=0&sdate=2016-12-12-00-00-00&edate="+edate+"&username=***&passhash=****", function(resp) {
var feat = resp.histdata,
tableData = [];
// Iterate over the JSON object
//String id = UUID.randomUUID().toString();
for (var j = 0, len = feat.length; j < len; j++) {
tableData.push({
//"no" : j,
"ids" : id_temp,
"datetime": feat[j].datetime,
"value": feat[j].value,
"value_raw": feat[j].value_raw,
"coverage": feat[j].coverage,
"coverage_raw": feat[j].coverage_raw
});
}
table.appendRows(tableData);
});
};
doneCallback();
};
The problem is that id_temp only gets set with the last value because { is in the wrong place.
This:
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
var id_temp = ids[i];
{
Should be:
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
{
var id_temp = ids[i];
//... rest of loop ...
its the issue that your entire code is not inside for loop , your for loop only have one line
use
var ids = ["3775", "6697"]
for ( i = 1; i < ids.length; i++)
{
var id_temp = ids[i];
$.getJSON("https://****/api/historicdata.json?id="+id_temp+"&avg=0&sdate=2016-12-12-00-00-00&edate="+edate+"&username=***&passhash=****", function(resp) {

JQuery .find() function for multiple array

Here is my code snippet
for (var k = 0; k < link_list.length; k++) {
var service_list = document.getElementsByName("service_info");
service_list = $(service_list).children("div[name=service_info_element]");
for (var i = 0; i < service_list.length; i++){
var service_info = {};
service_info["service_name"] = $(service_list[i]).find("select[name=service_name]").val();
service_info["service_type"] = $(service_list[i]).find("input[name=service_type]").val();
}
How can I get $(service_list[i]).find("select[name=service_name]").val() and $(service_list[i]).find("input[name=service_type]").val(); for each link_list[k] inside the second loop. I mean I need something like link_list[k].service_list[i].find("select[name=service_name]").val()
You can try this it'll work
$('service_list[i]').find('select[name=service_name]').filter([0,3,4]).anything();
Try the following:
link_list[k].service_list[i].find("select[name=service_name]").each(function(i, element){
var val = $(this).val(); // or element.val()
});

Create arrays dynamically in a loop with javascript

Here's a pseudocode example about what I'm trying to do:
var totalLanguages = XX;
for(var i = 0; i < totalLanguages; i++){
var dynamicArray + i = new Array();
/*.....*/
}
I need to create dynamically many arrays as the value of totalLanguages which can be either number.
This is to be able to do something like this:
for(var i = 0; i < totalLanguages; i++){
var arrayLanguages["es"] = dynamicArray+i;
var arrayLanguages["en"] = dynamicArray+i;
}
Is there any way to do this?
var languageNames = ['en', 'es'];
var languages = {};
for (var i = 0; i < languageNames.length; i++) {
languages[languageNames[i]] = [];
}
You are basically trying to recreate an array with variable names. Just use an Array to start out!
var dynamicArray = [];
for(var i = 0; i < totalLanguages; i++) {
dynamicArray[i] = new Array();
}
You can use multi-dimensional arrays:
var languagesArray = new Array(totalLanguages);
for(var i = 0; i < totalLanguages; i++) {
var innerArray = new Array();
innerArray.push("Hello");
innerArray.push("World");
languagesArray[i] = innerArray;
}
console.log(languagesArray[0][0]);
See: How can I create a two dimensional array in JavaScript?
How about:
for(var i = 0; i < totalLanguages; i++){
window["dynamicvariable " + i] = new Array();
/*.....*/
}

Handling multidimentional array in java script is not working

function split(str)
{
var array = str.split(';');
var test[][] = new Array();
for(var i = 0; i < array.length; i++)
{
var arr = array[i].split(',');
for(var j = 0; j < arr.length; j++)
{
test[i][j]=arr[j];
}
}
}
onchange="split('1,2,3;4,5,6;7,8,9;a,b,c;d,e,f;g,h,i')"
it was not working. i need to split this string to 6*3 multi dimentional array
var array[][] = new Array() is not valid syntax for declaring arrays. Javascript arrays are one dimensional leaving you to nest them. Which means you need to insert a new array into each slot yourself before you can start appending to it.
Like this: http://jsfiddle.net/Squeegy/ShWGB/
function split(str) {
var lines = str.split(';');
var test = [];
for(var i = 0; i < lines.length; i++) {
if (typeof test[i] === 'undefined') {
test[i] = [];
}
var line = lines[i].split(',');
for(var j = 0; j < line.length; j++) {
test[i][j] = line[j];
}
}
return test;
}
console.log(split('a,b,c;d,e,f'));
var test[][] is an invalid javascript syntax.
To create a 2D array, which is an array of array, just declare your array and push arrays into it.
Something like this:
var myArr = new Array(10);
for (var i = 0; i < 10; i++) {
myArr[i] = new Array(20);
}
I'll let you apply this to your problem. Also, I don't like the name of your function, try to use something different from the standards, to avoid confusion when you read your code days or months from now.
function split(str)
{
var array = str.split(';'),
length = array.length;
for (var i = 0; i < length; i++) array[i] = array[i].split(',');
return array;
}
Here's the fiddle: http://jsfiddle.net/AbXNk/
var str='1,2,3;4,5,6;7,8,9;a,b,c;d,e,f;g,h,i';
var arr=str.split(";");
for(var i=0;i<arr.length;i++)arr[i]=arr[i].split(",");
Now arr is an array with 6 elements and each element contain array with 3 elements.
Accessing element:
alert(arr[4][2]); // letter "f" displayed

Categories

Resources