Dave's Brain

Browse - programming tips - convert jquery handle to from javascript

Date: 2009jul26
Language: javaScript
Platform: jQuery

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;
	});

	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 ---
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.