0

I have a <table> which is dynamically created so the width is unknown. I'm trying to add horizontal scroll bars to the top and bottom of it. My current code...

<div class="wrapper1">
    <div class="div1" width="<script type=\"text/javascript\">document.write(mytext);</script>">
    </div>
</div>

<div class="wrapper2">
   <div class="div2">
      <table id="table_width">
         *table content goes here*
      </table>
   </div>
 </div>

<script type="text/javascript">
$(function(){
$(".wrapper1").scroll(function(){
    $(".wrapper2")
        .scrollLeft($(".wrapper1").scrollLeft());
});
$(".wrapper2").scroll(function(){
    $(".wrapper1")
        .scrollLeft($(".wrapper2").scrollLeft());
});
});

var mytext = document.getElementById("table_width").offsetWidth; //Method 1
var mytext = document.getElementById("table_width").style.width = width + 'px'; //Method 2
</script>

In the last few lines, I've tried two different methods to set the width of the containing <div> to be mytext.

Any ideas why either method isn't working? BTW, I'm not trying both methods at the same time the way it's shown here.

3
  • You have no element with ID table_width Commented Oct 24, 2015 at 18:46
  • I'm not seeing an element in your HTML named table_width. Commented Oct 24, 2015 at 18:46
  • Forgot to add that in... sorry. It's there now though. Commented Oct 24, 2015 at 18:52

3 Answers 3

1

To find the width of an element you need to use window.getComputedStyle followed by getPropertyValue on that returned value. What you are doing now only gives you the CSS value which is not set.

https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

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

1 Comment

I ended up using part of your suggest solution and part of @Avinash Kadimisetty's. Thus, the upvote for your answer. Thanks.
1

The problem could be this.

The script you have written to find mytext is being executed after the table is created. But by the time the div is created value of mytext is not known. So I would recommend doing this.

<!DOCTYPE html>


<div class="wrapper1">
   <div id="div1">
   </div>
</div>

<div class="wrapper2">
 <div class="div2">
   <table>
     *table content goes here*
   </table>
 </div>

 <script type="text/javascript">
   $(function(){
       $(".wrapper1").scroll(function(){
          $(".wrapper2")
      .scrollLeft($(".wrapper1").scrollLeft());
   });
$(".wrapper2").scroll(function(){
$(".wrapper1")
    .scrollLeft($(".wrapper2").scrollLeft());
 });
});

var mytext = document.getElementById("table_width").offsetWidth; //Method 1
var mytext = document.getElementById("table_width").style.width ; //Method 2

 document.getElementById('div1').width = mytext;
 </script>


</html>

The above code changes the width of the div after finding the value of mytext.

2 Comments

This doesn't seem to be working on my end, but I see what you're trying to do. Is there an error in your code maybe?
I ended up using part of your suggest solution and part of @Rob's. Thus, the upvote for your answer. Thanks.
0

I ended up using parts of both of the suggested answers for this question. After a lot of fiddling, I ended up with this for the final Javascript...

var elem = document.getElementById("table_width");
var mytext = window.getComputedStyle(elem,null).getPropertyValue("width");
$('#div1').css({'width':mytext});
$('#div1').css({'overflow': 'scroll'});

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.