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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting element attributes in a different way

    Hi anyone !

    My doubt is the following one...

    I would like to change the attributes of an element in such a way:

    Code:
    setElement('table1', {
        border : 0, 
        rows   : {
            0 : {
                cells : {
                    0 : {
                        innerHTML : 'foo',
                    },
    
                    1 : {
                        innerHTML : 'bar',
                    },
                },
            },
        },
    });
    This would be interpreted by the JavaScript as:
    Code:
    document.getElementById('table1').border = 0;
    document.getElementById('table1').rows[0].cells[0].innerHTML = 'foo';
    document.getElementById('table1').rows[0].cells[1].innerHTML = 'bar';
    Such function would be of much utility for me, therefore it would go to save much code...

    Somebody would know to say me as I can make this ?!

    Many thanks.

    Cheers,
    v1d4l0k4

    ----------

    Excuse-me for my poorly english, I'm brazilian and I don't speak english fluently.
    Last edited by v1d4l0k4; 07-30-2006 at 07:10 PM.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Try this and not to use so many misplaced commas:
    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    		Object.prototype.setProperties = function(props)
    		{
    			var x;
    			
    			for(x in props)
    			{
    				if(x in props.constructor.prototype)
    				{
    					continue;	
    				}
    				if(undefined == typeof this[x] || false == /^\s*function Object()/.test(props[x].constructor.toString()))
    				{
    					this[x] = props[x];
    				}
    				else
    				{
    					this[x].setProperties(props[x]);
    				}
    			}
    		}
    		
    		function setElement(id, props)
    		{
    			var element = document.getElementById(id);
    			
    			element.setProperties(props);
    			
    			return element;
    		}
    		
    		window.onload = function()
    		{
    			setElement(
    				'table1',
    				{
    					border : 10, 
    					rows :
    					{
    						0 :
    						{
    							cells :
    							{
    								0 :
    								{
    									innerHTML : 'foo'
    								},
    								1 :
    								{
    									innerHTML : 'bar'
    								}
    							}
    						}
    					}
    				}
    			);
    		}
    		</script>
    	</head>
    	<body>
    		<table id="table1">
    			<tr>
    				<td>xxx</td>
    				<td>yyy</td>
    			</tr>
    		</table>
    	</body>
    </html>
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

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