Jump to content

[SOLVED] onclick function not working as expected


gevans

Recommended Posts

Hey guys, I've written the following bit of javascript;

 

window.onload = function() {
    var elements = getElementsByClassName("listPropertyFocus");
    for(var i=0;i<elements.length;i++) {
        var fieldObj = elements.getAttribute("rel");
        var obj = document.getElementById(fieldObj);
        elements.onclick = function() {
            obj.focus();
        }
    }
}

 

The variable elements gets an array of object (mainly divs). Then as I loop through the elements array I assigned a function to each so that they function on a form field, the id of the form field is passed by the rel attribute of the original object.

 

At the moment every single object assigned with this function focuses on the last object passed by the last rel attribute. So if it was written out in fullish it'd be like this (working properly);

 

<input type="text" id="a_name" name="name" />
<input type="text" id="a_dob" name="dob" />
<div onclick="document.getElementById('a_name').focus()">
CLICKY
</div>
<div onclick="document.getElementById('a_dob').focus()">
CLICKY
</div>

 

but is resulting in something more like this;

 

<input type="text" id="a_name" name="name" />
  <input type="text" id="a_dob" name="dob" />
<div onclick="document.getElementById('a_dob').focus()">
CLICKY
</div>
<div onclick="document.getElementById('a_dob').focus()">
CLICKY
</div>

 

Hope all that made sense, any ideas?

Link to comment
Share on other sites

Just a note;

 

        var fieldObj = elements.getAttribute("rel");

 

elements.onclick = function() {

 

those two lines are actually

 

        var fieldObj = elements [ i ] .getAttribute("rel");

 

elements [ i ] .onclick = function() {

 

Without the spaces, but thew forum parses it and it dissapeared.

Link to comment
Share on other sites

Bit of shuffeling and it's all fixed,

 

fixed code;

 

window.onload = function() {
var elements = getElementsByClassName("listPropertyFocus");
for(var i=0;i<elements.length;i++) {
	elements[i].onclick = function() {
		var fieldObj = this.getAttribute("rel");
		var obj = document.getElementById(fieldObj);
		obj.focus();
	}
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.