1

I'm trying to move an image holder div to specific x and y coordinates after input from two separate text field inputs have been changed. Below is the code for just the x coordinate.

Here is a jfiddle here

var img1x;
var img1y;
var img1position;

img1position = $( "#img1" ).position();
img1x = img1position.left;
img1y = img1position.top;

I've tried this and it only moves the div if the imput value is "0". it won't move for anything else:

$("#img1x").change(function() {
  //alert("img1x: " +img1x);
    $( "#img1" ).css('left', $( "#img1x" ).val());
});

and I've tried this and it does nothing.

$("#img1x").change(function() {
  img1x = $( "#img1x" ).val();
  //alert("img1x: " +img1x);
    $( "#img1" ).css('left', img1x);
});
1
  • Andrew is right, you might try use api.jquery.com/animate for a fluid transition then you wouldn't need the px in the end Commented Jan 11, 2013 at 22:42

4 Answers 4

3

Try adding 'px':

$("#img1x").change(function() {
  img1x = $( "#img1x" ).val();
  alert("img1x: " +img1x);
    $( "#img1" ).css('left', img1x + "px");
});

Works for me: http://jsfiddle.net/VNqqx/

The reason 0 works is because left: 0 is valid CSS.

Sign up to request clarification or add additional context in comments.

1 Comment

Right, of course, I was forgetting "px"! Thanks! The cold medication is playing tricks on me.
1

Try adding 'px', or * 1 to get an int:

$( "#img1" ).css('left', img1x + 'px');

see http://jsfiddle.net/dhpkW/1/

Comments

1

Add a "px" to your left value:

$( "#img1" ).css('left', img1x+'px');

Comments

1

You could try this for fluid transition

$("#img1x").change(function () {
    img1x = $("#img1x").val();
    alert("img1x: " + img1x);
    $("#img1").animate({
        'left': img1x
    }, 700);
});

You don't need the px then.

Check it out http://jsfiddle.net/dhpkW/6/

Comments

Your Answer

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.