Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Pass a variable name as a value of a parameter of a function.

    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.)

    Code:
    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();
    	};
    }

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    .s and []s are pretty interchangeable in javascript, which means that:
    Code:
    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:
    Code:
    var var_name = 'title';
    //then you can use:
    rows[number][var_name];
    to do what you want. Note the lack of 's this time.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay View Post
    .s and []s are pretty interchangeable in javascript, which means that:
    Code:
    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:
    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •