Programming Tips - How do you convert a jQuery handle to/from a javaScript handle?

Date: 2009jul26 Language: javaScript Library: jQuery Keywords: FAQ Q. How do you convert a jQuery handle to/from a javaScript handle? A. Here are two function that do that and some code that demonstrates their use. --- start of test_convert.html ---
<script src=js/jquery.js></script> <style> span { border: thin solid brown; padding: 3px; } </style> Going from JS to jQuery: <span id=one>Start</span> <p> Going from jQuery to JS: <span id=two>Start</span> <script> function jQueryHandleToJsHandle(h) { var jsHandle; h.each(function() { jsHandle = this; }); // This works fine if the jQuery handle only refers to one object. // If it refers to mulitple, use an array. return jsHandle; } function jsHandleToJQueryHandle(h) { return $(h); } function testJsToJQuery() { var h1, h2; h1 = document.getElementById('one'); h1.innerHTML = 'Changed with JS handle'; h2 = jsHandleToJQueryHandle(h1); h2.html('SUCCESS - changed with jQuery handle'); } function testJQueryToJs() { var h1, h2; h1 = $('#two'); h1.html('Changed with jQuery handle'); h2 = jQueryHandleToJsHandle(h1); h2.innerHTML = 'SUCCESS - changed with JS handle'; } testJsToJQuery(); testJQueryToJs(); </script>
--- end of test_convert.html ---