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 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't use object properties' values

    Hi.
    I'm succesfully fetching an img element by it's ID, but I can't any of the properties' values, not even the id property.
    I tried it both in Firefox and IE6 but to no avail.

    Please help me solve this issue.
    Code:
    // fetch_object emulates document.getElementById
    // and bridges over browser compatibility (it works, the problem is not here)
    
    var buttonIcon = fetch_object(obj.id + '_icon');
    alert(buttonIcon.id);
    The fetching works, but again, I can't get any of the properties' values so the alert function isn't functioning. What's even more weird, I tried the following code and it worked:
    Code:
    for (property in buttonIcon)
    {
       alert(proprty + "<br>");
    }
    All of the properties' names were printed out. I think I am going crazy. Why can't I get the properties values?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Please post how the fetch_object() function looks like.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How do you know that the function is working correctly? If it actually does work as you say, then the code you posted should also work. Even if you think the function is good, I'm thinking we'll still need to see it to determine what the problem might be...
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Is "proprty" a typo?

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for the delay.
    Here's the function:
    Code:
    function fetch_object(idname)
    {
    	if (document.getElementById)
    	{
    		return document.getElementById(idname);
    	}
    	else if (document.all)
    	{
    		return document.all[idname];
    	}
    	else if (document.layers)
    	{
    		return document.layers[idname];
    	}
    	else
    	{
    		return null;
    	}
    }
    Actually I solved my problem. I had a loop searching for IDs to define objects using the IDs' names, and I forgot to set some of the IDs.

    But a new problem is blocking me.
    I'm defining a new property for an object in a funtion that 'initializes' (i.e. defining properties) for a set of objects.
    At the end of this function I'm setting a new event handler function to handle mosue events. everything works, except the event handler can't access the new property I added. The new property is an element. (I do it this way:
    Code:
    obj.icon = getelementbyid("id");
    )

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I have simplified your ideea and build a code which does what you want
    As you see, it works:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    onload=function(){
    var obj = document.getElementById('mydiv')
    obj.icon=obj.getElementsByTagName('img')[0]
    obj.icon.src='01.jpg'
    }
    </script>
    </head>
    <div id="mydiv">
    <img src="00.jpg">
    </div>
    </body>
    </html>
    On the other hand, I see that you use that fetch_object() to create objects according to the browsers modern/NS4/IE4

    You are waisting your time. Almost no one use NS4, and absolutely no one IE4 these days. Why bother to complicate the code?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, take a look at this, and tell me if you see anything wrong, please:
    Code:
    this.init_controls = function()
    // earlier in the script: this.controlbar = fetch_object('controls');
    	{
    		var controls = new Array();
    
            //fetch_tags emulates document.getElementsByTagName
    		var buttons = fetch_tags(this.controlbar, 'div');
    		for (var i = 0; i < buttons.length; i++)
    		{
    			if (buttons[i].className == 'imagebutton' && buttons[i].id)
    			{
    				controls[controls.length] = buttons[i].id;
    			}
    		}
    		for (var i = 0; i < controls.length; i++)
    		{
    			var control = fetch_object(controls[i]);
    			this.init_command_button(control);
    		}
    	};
    Code:
    	this.init_command_button = function(obj)
    	{
    		obj.cmd = obj.id.substr(obj.id.indexOf('_cmd_') + 5);
    		obj.editorid = this.editorid;
    		this.buttons[obj.cmd] = obj;
    		obj.state = false;
    		obj.mode = 'normal';
    
            // icon handlers
            obj.icon = fetch_object(obj.id + '_icon');
    
    		// event handlers
    		obj.onclick = obj.onmousedown = obj.onmouseover = obj.onmouseout = Command_button_onmouseevent;
    	}
    Code:
    Command_button_onmouseevent = function(e)
    {
    if (e.type == 'click')
    	{
    		format(e, this.cmd, false, true);
    	}
    
    	button_context(this, e.type);
    };
    Code:
    button_context = function(obj, state, controltype)
    	{
            // This function decides what style-state should be set for the current event
            // I just thought I'd save reading theit's code for you
            // Code summary:
            set_control_style(obj, controltype, 'state');
        };
    Code:
    set_control_style = function(obj, controltype, mode)
    	{
            var properties = '';
            for (property in obj.icon)
            {
            properties += property + '\n';
            }
            alert(properties);
            obj.icon.src='dude.gif';
    		if (obj.mode != mode)
    		{
    			obj.mode = mode;
    
    			// This is where the button's style is set
            }
    The controlbar is built with many buttons whom syntax is like this:
    Code:
    <td id="controls" class="controlbar" colspan="2">
        <div class="imagebutton" id="vB_Editor_001_cmd_italic">
            <img id="cmd_[cmd]_icon" title="[stc]" src="[src]">
        </div>
    </td>
    Last edited by pilau; 08-08-2006 at 08:46 PM.


  •  

    Posting Permissions

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