adding a parameter to a button input

<script>          
            $("#leftButton").click(function () {
                  moveObject('left', numberOfPixels);
            });
</script>
<input type="button" id="leftButton" value="Left" />


The snippet above is used to move an object on the screen. I would like to do the same with more that one object. I  think I need to do something like:

<script>          
            $("#leftButton").click(function (objID) {
                  moveObject('left', numberOfPixels,objID);
            });
</script>

If this is the way to go, how do you pass a parameter with an input button?
Gary SamuelsPlant ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy PooleCommented:
you wouldn't pass a parameter.  Are you trying to move all objects at the same time?  Selected objects?  Or?
0
Gary SamuelsPlant ManagerAuthor Commented:
just a selected objects, one at a time. The buttons are within a form so I can't change the type. It works great for a single object.

Here's the full code:
<script>
      $(document).ready(function() {
            var numberOfPixels = 20;
           
            $("#downButton").click(function () {
                  moveObject('down', numberOfPixels);
            });
           
            $("#upButton").click(function () {
                  moveObject('up', numberOfPixels);
            });
           
            $("#leftButton").click(function () {
                  moveObject('left', numberOfPixels);
            });
           
            $("#rightButton").click(function () {
                  moveObject('right', numberOfPixels);
            });
      });
     
      function getPosition() {
            return $('#graphicCount1').position();
      }
     
      function moveObject(direction, pixels) {
            var position = getPosition();
            var top = position.top;
            var left = position.left;
           
            if (direction == 'down') {
                  top += pixels;
            }
            else if (direction == 'up') {
                  top -= pixels;
            }
            else if (direction == 'right') {
                  left += pixels;
            }
            else if (direction == 'left') {
                  left -= pixels;
            }
            var imageElement = $('#graphicCount1');
            $(imageElement).animate({
                        'top' : top + 'px',
                        'left' : left + 'px'
                  }, 500, function() {});
      }
</script>

        <td class="moveposition" width="25%">
                  Move:
                  <input type="button" id="downButton" value="Down" />
                  <input type="button" id="upButton" value="Up" />
                  <input type="button" id="leftButton" value="Left" />
                  <input type="button" id="rightButton" value="Right" />        
        </td>
        <td class="moveamount" width="25%">
                  Amount to Move:
                  <input type="button" id="downAmount" value="Down" />
                  <input type="text"  id="moveamount" value="20" size="1">
                  <input type="button" id="upAmount" value="Up" />
        </td>
0
Gary SamuelsPlant ManagerAuthor Commented:
I.O.U., I think this was your code from last week and I couldn't get it to work until I figured out I needed to load jquery. I was loading javascript.
0
Randy PooleCommented:
What I mean
<script>
      $(document).ready(function() {
            var numberOfPixels = 20;
            
            $("#downButton").click(function () {
                  moveObject('down', numberOfPixels);
            });
            
            $("#upButton").click(function () {
                  moveObject('up', numberOfPixels);
            });
            
            $("#leftButton").click(function () {
                  moveObject('left', numberOfPixels);
            });
            
            $("#rightButton").click(function () {
                  moveObject('right', numberOfPixels);
            });
      });
      
      function getPosition(id) {
            return $(id).position();
      }
      
      function moveObject(direction, pixels,id) {
      			// if no id is passed then use original
      			id=!id?'#graphicCount1';
            var position = getPosition(id);
            var top = position.top;
            var left = position.left;
            
            if (direction == 'down') {
                  top += pixels;
            }
            else if (direction == 'up') {
                  top -= pixels;
            }
            else if (direction == 'right') {
                  left += pixels;
            }
            else if (direction == 'left') {
                  left -= pixels;
            }
            var imageElement = $(id);
            $(imageElement).animate({ 
                        'top' : top + 'px',
                        'left' : left + 'px'
                  }, 500, function() {});
      }
</script>

        <td class="moveposition" width="25%">
                  Move:
                  <input type="button" id="downButton" value="Down" />
                  <input type="button" id="upButton" value="Up" />
                  <input type="button" id="leftButton" value="Left" />
                  <input type="button" id="rightButton" value="Right" />        
        </td>
        <td class="moveamount" width="25%">
                  Amount to Move:
                  <input type="button" id="downAmount" value="Down" />
                  <input type="text"  id="moveamount" value="20" size="1">
                  <input type="button" id="upAmount" value="Up" />
        </td>

Open in new window

With this code you can now call the object name you would like to call:
moveObject('down', numberOfPixels,'#graphicCount1' );

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.