Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

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);
    }
);
share|improve this answer
add comment

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

share|improve this answer
    
why -1? Is it because I didn't put any code example? –  kaklon Jan 20 '11 at 12:48
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.