Best Practice To Consolidate This Reusable Code?

Best Practice To Consolidate This Reusable Code?

Hello all. 
I have the following code that allow me to use the same functions if dropped within any one div. Click that div and it returns it coordinates, ht, and width.

Instead of placing the same code block over and over. I would like to make it a simple function call but am not sure at which point to pass the "this" value. 

Please review the following. Thanks

  1. <script>
  2. //THIS FILLS IN THE TEXT FIELDS WITH THE PROPERTIES OF THE DIV CLICKED
  3. function rw_setTxtField(widthPassed,heightPassed, posLeftPassed, posTopPassed){
  4. document.formRW.rw_findWidthField.value = widthPassed;
  5. document.formRW.rw_findheightField.value = heightPassed;
  6. document.formRW.rw_findLeftPosField.value = posLeftPassed;
  7. document.formRW.rw_findTopPosField.value = posTopPassed;
  8. }
  9. </script>
  10. <!-- THIS SIMPLY PROVIDES THE TEXT FIELDS TO PLACE THE VALUES-->
  11. <div id="div_inspector">
  12. <form id="formRW" name="formRW">
  13.        Width: <input name="rw_findWidthField" id="rw_findWidthField" type="text"  />
  14.        Height: <input name="rw_findheightField" id="rw_findheightField" type="text"  />
  15.        Left Position: <input name="rw_findLeftPosField" id="rw_findLeftPosField" type="text"  /
  16.    Top Position: <input name="rw_findTopPosField" id="rw_findTopPosField" type="text"  />
  17.         </form>
  1. </div><!--END DIV INSPECTOR-->

  2. <div id="div_getPosition1" style="background-color:#333333; width:300px; height:30px; position:relative; left:40px;">
  3. <script>
  4. //HOW DO I OPTIMIZE THIS PART BY ONLY HAVING TO PLACE IT ONCE. 
  5.         // IT IS CURRENTLY DYNAMIC WITH THE ABILITY TO USE "THIS"
  6. $("div").click(function(){
  7. var myTarget = $(this);
  8. var myWidth = myTarget.width();
  9. var myHeight = myTarget.height();
  10. var myPosition = myTarget.position();
  11. var finalLeftPosition = myPosition.left;
  12. var finalTopPosition = myPosition.top;
  13. rw_setTxtField(myWidth,myHeight,finalLeftPosition, finalTopPosition);
  14. break;
  15. });
  16. </script>
  17. </div><!-- END DIV div_getPosition1-->


  18. <div id="div_getPosition2" style="background-color:#333333; width:600px; height:30px;">
  19. <script>
  20. $("div").click(function(){
  21. var myTarget = $(this);
  22. var myWidth = myTarget.width();
  23. var myHeight = myTarget.height();
  24. var myPosition = myTarget.position();
  25. var finalLeftPosition = myPosition.left;
  26. var finalTopPosition = myPosition.top;
  27. rw_setTxtField(myWidth,myHeight,finalLeftPosition, finalTopPosition);
  28. break;
  29. });
  30. </script>
  31. </div><!-- END DIV div_getPosition2 -->