javascript - Set correct context for focus(); -


How do I get the correct context set when I want to apply Focus ()?

I try to do this basically:

  elemnt = document.getElementById ('someFormField'); Elemnt.focus ('none');  

The call to focus is generated, queued and applicable only in the script elsewhere when the application requests it.

  function testIt () {var queued = {ElementId: 'someFormField', joy: focus, args: ['none']}; Elemnt = document.getElementById (queued.lment id); Queued.func.apply (elemnt, queued.args); }  

The above method works for another function but I get an error for the Focus method:

Opera: WRONG_THIS_ERR < Firefox: Untold exception: [exception ... "Invalid operation on RapedNenet prototype object" (...)]



< Strong> How can it get to work?

(I'm trying to understand this issue, so I'm not interested in the 'Use Framework X' answer)



> Update:
There is some confusion about why I pass an argument for Focus () : only to pass that argument An argument for the Focus () was included will not be an error.

I can type just as well:

  document.getElementById ('someFormField') .focus ();  



Update (2):
Because the answer does not work in IE6, I now have a problem Intermediate task to solve:

  function testIt () {var queued = {elementId: 'someFormField', function: setFocus, args: [someFormField ']}; Elemnt = document.getElementById (queued.lment id); Queued.func.apply (elemnt, queued.args); } Function Set Focus (elemntId) {document.getElementById (elemntId). Focus (); }  

If your requirement letter does not have IE6 compatibility,

< P> What works in the interests of avoiding confusing yourself, as long as I do not need to reduce memory usage, I will always start with a closing:

  var dofocus = function () {element.focus ()}; Dofocus ();  

If you are in a loop and changing the element , then you can use that function which returns another function: ( Function (e, m) {Return function () {e [m] ()}}) (element, method)

edit : question again : On reading, I am thinking your code is not working because of this, because you are passing in the value of some value named 'Focus', not the string method 'Focus' or the Focus function (it's somewhere Au Define?)

Edit : it works:

  & lt; Html & gt; & Lt; Title & gt; Trial & lt; / Title & gt; & Lt; Script & gt; Function foo (id, method, args) {var d = {"id": id, "method": method, "args": args}; // time pass var e = document.getElementById (d.id); // exam exists f f = e [d.method]; // exam exists a = d.args; // Exam is an array f.apply (e, a); } & Lt; / Script & gt; & Lt; Body id = "thebody" & gt; & Lt; P & gt; & Lt; Button onclick = "foo ('bar', 'set selection archive', [4,6]);" & Gt; Select & lt; / Button & gt; & Lt; Button onclick = "foo ('bar', 'focus', []);" & Gt; Focus & lt; / Button & gt; & Lt; Input type = "text" id = "bar" value = "foo bar baz" & gt; & Lt; P & gt; & Lt; Button onclick = "foo ('thebody', 'appendChild', [document.createTextNode (new date ());" & Gt; Body & lt; / Button & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

Edit : And if the function is on, then you can always pass the name of an alternative method and check that the method string or the function Is able to use 'Focus' or Custom Function without having to keep on the element again.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -