tags:

views:

108

answers:

2

Hi there,

I have a requirement to have a small form with some text boxes on them and a button at the end which with a specified aspect ratio (in hidden textbox) enable the user to type in a value into the width box for example and when the user changes the value the height textbox will change also..also when you type in the height box the width will change. I do not want someone to do this for me, but i would require some help setting up the jquery functions to run when i change the text in a partiqular textbox.

Any help, guidance would be fantastic..many thanks

Chris

A: 

Hi Chris. Welcome to stackoverflow.

You can do something like this:

$('#widthBoxId').focusout(function(){
  var height = $(this).val() //math calculations based on $('#hiddenRatioId')
  $('#heightBoxId').val(height);
});

You can then do the reverse with the height box.

jeerose
fantastic..i will try that very soon..ill let you know how i get on.. :)...can i just confirm..the $(this).val()..is this a function? do i need to change this to perform the action or can i extract this into a function.. ?
Please do, and remember to select the correct answer (even if it's not mine!). Keeping your accept rate high will encourage others to help you next time.
jeerose
$(this).val() refers to the current value of $('#widthBoxId') since you're inside a event handler ( .focusout() ) for that element.
jeerose
A: 

This is wherei have got to now..very nearly there..but is there an easy way to round the numbers?

<!DOCTYPE html>
<html>
<head>
  <script src="scripts/jquery-1.3.2.min.js"></script>
</head>
<body>

<input type='text' id='aspectratio' />
<input type='text' id='width' />
X
<input type='text' id='height' />
  <div></div>
<script>
$('#width').keyup(function(){
  var aspectratio = $('#aspectratio').val();
  var width = $(this).val();
  var height = aspectratio * width;
  $('#height').val(height);
});

$('#height').keyup(function(){
  var aspectratio = $('#aspectratio').val();
  var height = $(this).val();
  var width = aspectratio / height;
  $('#width').val(width);
});

</script>
</body>
</html>
Chris, you should update your original post by indicating that it's an update rather than posting an answer... but you can round down by using Math.floor(number) where number is the number you want to round.
jeerose
thanks for the info...im loving this site though...very useful :)
It's a fantastic resource. Be sure to have a look at the FAQs: http://stackoverflow.com/faq
jeerose