4

This JS script gives me an alertbox saying;

jQuery.UI.version = 1.0.2  ---   Box Width: undefined

How can I select the #resizable?

 <head>
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script src="jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script>
       <style>
  #resizable { width: 200px; height: 150px; padding: 5px; border: 1px solid red;}

  </style>

      <script>
        $(function () {
            var boxWidth = $('#resizable').attr('width');
            alert("jQuery.UI.version = " + jQuery.ui.version + "  ---   Box Width: " + boxWidth);           
        });
  </script>


</head>
<body>
    <div id="resizable" class=">
        <div id="title">
            <h2>
            The expanding box
            </h2>
        </div>    
    </div>
</body>
1
  • 1
    As you can see, the problem is not with selecting the element (that works fine), it's about accessing the information (width) properly. Just wanted to point this out explicitly. Commented Apr 11, 2013 at 10:04

6 Answers 6

3

See here you don't have a attribute of width:

<div id="resizable" class=">

this will surely crash and set to undefined, var boxWidth = $('#resizable').attr('width');

There are two ways getting the width of an element like:

By the .width() way:

$('#resizable').width();

and by .css() way:

$('#resizable').css('width');
Sign up to request clarification or add additional context in comments.

Comments

3

your selector $('#resizable').attr('width'); search for attribute width in resizable which does not exists

i think you are asking for width()

try this

 var boxWidth = $('#resizable').width(); //this gives you the width of resizable div

updated seeing comment

your code reads the attribute width of div <div id="resizable" class="asd"> ..but see , there is no attribute called width in this div so it fails... doc to read more about attr()

your code will work if you add width in div like <div id="resizable" class="asd" width="30px">...this will give you 30px as alert...

3 Comments

Thanks! How come this works and my dont? I got my code from the jQuery site.
@Lautaro: Your div element don't have a width attribute. Only id and class. Even if you would set a custom width attribute, it would just give you the value you set, not the actual width of the element.
@Lautaro i have explained it in my answer.....anyways your code reads the attribute width of div <div id="resizable" class="asd"> ..but see theres is no attributes called width in this div... your code will work if you add width in div like <div id="resizable" class="asd" width="30px">...this will give you alert as 30px
1

try this bit:

$('#resizable').width();

Comments

1

You dont have a width attribute on that div, are you trying to get the width property of the div? try

$("#resizable").width();

Comments

0
alert($('#resizable').width()); will you give 200

alert($('#resizable').css('width')); and this will you give 200px 

div width

Comments

0

With Pure JavaScript you can do this in following way:

document.getElementById('resizable').clientWidth;

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.