gevans Posted February 16, 2009 Share Posted February 16, 2009 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? Quote Link to comment Share on other sites More sharing options...
gevans Posted February 16, 2009 Author Share Posted February 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
gevans Posted February 16, 2009 Author Share Posted February 16, 2009 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(); } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.