views:

60

answers:

3

I'm trying to display the "right" value of a div named "slider", displaying it inside of "span.display_value"

    var rightStyleValue = $('div.slider').css('right');
 alert(rightStyleValue); 
 $('.display_value').html(rightStyleValue);


UPDATE: Html is posted below. I'm using a drag and drop script to drag a div called "slider" that is inside of a div named "container". I want to have it "do something" when the "right" property of the div "slider" reaches 45px(ideally, when the user drags "slider" all the way to the right side of it's parent "container")

Right now, when the page loads it will display the value that i specifically set in the css: "Right:-10". so it will display it once, but i'm not sure how to keep updating the displayed value for the "slider" div.

i don't know where to put: $('.display_value').html(rightStyleValue); to have it continually updated.


<head>
 <style>
  div.container {
  height:15px;
  width:200px;
  overflow:hidden;
  background-color:#858585;
  }

  div.slider {
  height:15px;
  width:20px;
  background-color:#000;
  position:relative;
  right:-10px;
  }
 </style>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/easing.js"></script>      
<script type="text/javascript" src="js/dragcopy.js"></script>  



<script>

 $().ready(function() {
  $('.slider').jqDrag();

  $('.slider')
  .jqDrag();
 });

 $(document).ready(function() {

  var rightStyleValue = $('div.slider').css('right');
  alert(rightStyleValue); 
 $('.display_value').html(rightStyleValue); 

 $(".slider").mousedown(function() {
     if ($('.slider').css('right') == -8 + "px") {
             $('.container').fadeOut('slow');
     } 

 });
 });




 </script>

</head>
<body>




 <div class="container">
  <div class="slider">

  </div>
 </div>
 <p>slider right:<span class="display_value"> </span></p>




</body>

A: 

Looks like you're having extra bracers after css method call. Also I think val should be replaced with text. Here is example:

$('span.display_value').text($('div.slider').css('right'));
Ivan Nevostruev
it still doesn't work. is the syntax correct?
ExodusNicholas
Try to replace `val` with `html`.
Ivan Nevostruev
Or also replace val with text($('div.slider').css('right')) since it is just css.If thats not working I'd alert $('span.display_value').length and $('div.slider').css('right').
benjynito
@benjynito: Thanks for update
Ivan Nevostruev
I updated the question with code, and posted a comment to lameduck, any suggestions?
ExodusNicholas
+1  A: 

I think @Ivan Nevostruev is right. Just to suggest one other logical step. Your problem code is doing two things, getting the right style value and setting it to another element.

You should break these down to two separate steps to find which is causing your error ie:

var rightStyleValue = $('div.slider').css('right');
alert(rightStyleValue);

$('span.display_value').val(rightStyleValue);

Then, once you know which action is causing the problem you can break it down further, by testing is your selector working, ie:

alert($('div.slider'));

If the result is null or undefined, then you have a problem with your selector.

@Ivan is probably right, but without seeing your html, it could be a bunch of problems. Breaking it down always helps me.

Michael La Voie
Your code worked, i was using .val before and i changed it to .html and it worked.(.text works too). But the displayed value isn't changing when i mousedown on the slider. I put the code you gave me into the mousedown event but that didn't update the displayed number either, even though all the code you gave me was in the mousedown event. I would assume that every time I mousedown it should show the new css "right" value, but it doesn't. any suggestions?
ExodusNicholas
+2  A: 
$('span.display_value').text($('div.slider').css('right'));

use .text(x), not .html() or .val()

blesh