...

View Full Version : Pass a variable name as a value of a parameter of a function.



Scriptdaemon
05-01-2007, 08:27 AM
Not sure how great that explains what I am trying to do, but I want to pass as a value of a function either row[number].name or row[number].title depending on where the function is called from so it will edit the specific property of the object that I want correctly. Simply put, how can I combine these two functions? (I apologize for the probable horrible explanation, but I couldn't think of how to explain it better.)




function editName(number) {
if(edited)
return;
var tD = document.getElementById("nameTd" + number);
tD.style.padding = "0px";
tD.innerHTML = "<input type=\"text\" value=\"" + rows[number].name + "\" />";
var field = tD.childNodes[0];
field.select();
var save = function() {
var input = field.value.replace(/^\s*|\s*$/g, "");
if(input.length == 0)
input = rows[number].name;
rows[number].name = unEscapeText(input);
createTable();
};
field.onblur = function() {
save();
};
field.onkeydown = function(event) {
if(isEnter(event))
save();
};
}
function editTitle(number) {
if(edited)
return;
var tD = document.getElementById("titleTd" + number);
tD.style.padding = "0px";
tD.innerHTML = "<input type=\"text\" value=\"" + rows[number].title + "\" />";
var field = tD.childNodes[0];
field.select();
var save = function() {
var input = field.value.replace(/^\s*|\s*$/g, "");
if(input.length == 0)
input = rows[number].title;
rows[number].title = unEscapeText(input);
createTable();
};
field.onblur = function() {
save();
};
field.onkeydown = function(event) {
if(isEnter(event))
save();
};
}

GJay
05-01-2007, 08:46 AM
.s and []s are pretty interchangeable in javascript, which means that:


rows[number].title
//and
rows[number]['title']

do the same thing. Note the 's in the second though, they're important.

If instead you have:


var var_name = 'title';
//then you can use:
rows[number][var_name];

to do what you want. Note the lack of 's this time.

Scriptdaemon
05-01-2007, 08:52 AM
.s and []s are pretty interchangeable in javascript, which means that:


rows[number].title
//and
rows[number]['title']

do the same thing. Note the 's in the second though, they're important.

If instead you have:


var var_name = 'title';
//then you can use:
rows[number][var_name];

to do what you want. Note the lack of 's this time.

Wow, that was so simple. I can't believe I didn't think of that. Thanks a lot.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum