0

I've a form.

<form action="inc/genxml.php" method="post">
 <input id="nameTxt" name="name" type="text" value="test"/>
 <button id="nameSave" class="left">Save</button>
</form>

And a div element #name

When I click the save button, I want to pass the position of the div #name to the form action file. To get the position, I'm using jQuery .position().

Something like below. (which just prints out the coordinates)

$('#nameSave').click(
 function() {
 var pos = $('#name').position();
 alert("left: " + pos.left + ", top: " + pos.top );
 }
);

I want to pass the coordinate values (pos.left & post.top) to the form action file (in this case to the file genxml.php).

How should I do that?

Lightness Races in Orbit
387k77 gold badges673 silver badges1.1k bronze badges
asked Jan 20, 2011 at 12:36

3 Answers 3

7

The easiest way would be to use a hidden input field and use jQuery to set the value for this input field or these input fields in your case.

HTML:

<form action="inc/genxml.php" method="post">
 <input id="nameTxt" name="name" type="text" value="test"/>
 <input id="posLeft" name="posLeft" type="hidden" />
 <input id="posRight" name="posRight" type="hidden" />
 <button id="nameSave" class="left">Save</button>
</form>

JS:

$('#nameSave').click(
 function() {
 var pos = $('#name').position();
 $('#posLeft').val(pos.left);
 $('#posRight').val(pos.right);
 }
);
answered Jan 20, 2011 at 12:38
Sign up to request clarification or add additional context in comments.

Comments

2

add two hidden input to your form and use jQuery to change their value

answered Jan 20, 2011 at 12:38

Comments

1

Try this:

var pos = $('#name').position();
$("form").append('<input type="hidden" name="name_position" value="' + pos.left + ',' + pos.top + '" />');

Then read name_position value from the POST data in the server side code.

answered Jan 20, 2011 at 12:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.