views:

39

answers:

4

Stripped a ton of stuff to make it more readable, it throws an error on the line:

$('[data-weight]').each(function() {

Saying that it is null

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
    1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="ctl00_mainHead">

        <script type="text/javascript" src="includes/jqueryv1.4.2.js"></script>

           <script type="text/javascript">
            jQuery(document).ready(function() {
                $('[data-weight]').each(function() {

                    var usingMetric = false;

                    var $this = $(this);
                    var value = $this.attr("data-weight");
                    if (usingMetric) {
                        $this.text(value + " KG");
                    }
                    else {
                        value = parseFloat(value) * 2.20462262; // Convert to imperial
                        $this.text(value + " lbs");
                    }
                });
            });
        </script>
     </head>

    <body>

    <form name="aspnetForm" method="post" action="viewProduct.aspx?ID=3&amp;action=added" id="aspnetForm">


        <div class="productDetail">
            <b>Product Details</b>
        </div>

        <strong data-weight="200">800 KG</strong>


</form>
    </body>
</html>

Update

It's a follow on my this question:

http://stackoverflow.com/questions/3749900/using-javascript-to-display-weights

A: 

May i know,what is 'data-weight' ? Is it a control ID? If it is control id then you need to give :

$('#data-weight').children().each(function() {...

Or

The following post might be helpful to you :

http://stackoverflow.com/questions/91518/jquery-attribute-selectors-how-to-query-for-an-attribute-with-a-custom-namespace

Siva Gopal
Don't you see `<strong data-weight="200">`?
KennyTM
+2  A: 

data-weight is not a valid attribute, on before HTML5. How about doing it this way,

html part,

<strong class="weight-800">800 KG</strong>

jQuery part,

jQuery(document).ready(function () {
    $('[class^="weight"]').each(function () {

        var usingMetric = false;

        var $this = $(this);
        var value = this.className.split('-')[1];
        if (usingMetric) {
            $this.text(value + " KG");
        }
        else {
            value = parseFloat(value) * 2.20462262; // Convert to imperial
            $this.text(value + " lbs");
        }
    });
});

demo

Reigel
`data-weight` is valid as of HTML5. Since he's declaring XHTML 1.1, I *think* you're technically correct, but I can't imagine a browser actually caring. But still, nice edit for being valid pre-HTML5.
T.J. Crowder
This gives the exact same error
Tom Gullen
Are you sure about that `this.class().split('-')[1];` line? I'd think it should be `this.className.split('-')[1];` (e.g., `className` rather than `class` -- which is an invalid identifier in JavaScript -- and a property, not a function).
T.J. Crowder
@T.J. Crowder - haha fixed that, thanks... @Tom Gullen, please try... I have some corrections made.
Reigel
I've created a variant of the live example of my answer to Tom's original question inspired by this: http://jsbin.com/icure3/2
T.J. Crowder
@T.J - hmm cool 8)
Reigel
@Reigel: LOL, we should have teamed up -- I see you did a jsFiddle. :-)
T.J. Crowder
Thanks for the answer Reigel, it's a good demo I've +1 you but TJ had the solution to the error
Tom Gullen
+2  A: 

If you're using jQuery's "no conflict" mode, the problem might be this:

jQuery(document).ready(function() {
                                ^-- Consider adding $ here

...or use jQuery rather than $ throughout.

If you're not using jQuery no-conflict, I'm not seeing why there'd be a problem. It works just fine here: http://jsbin.com/odaxa3 The only edits I made were to load jQuery from Google's CDN and to make the weight in the attribute match the display.

T.J. Crowder
Thanks for the simplified example I'll try and work out why this isn't working
Tom Gullen
Ah adding the $ in the ready function works fine, thanks again! :=)
Tom Gullen
+2  A: 

Instead of converting the units, you could print all units and only display the one that is preferred:

<strong><span class="metric">800 kg</span> <span class="units-separator">/</span> <span class="imperial">1763 lbs</span></strong>

Now you can switch between the units by displaying/hiding the elements with the class metric‍/‍imperial:

.units-separator { display: none }
/* for metric view */
.metric { }
.imperial { display: none }
/* for imperial view */
.metric { display: none }
.imperial { }

And if CSS is not supported, both units are shown as:

800 kg / 1763 lbs

Gumbo
Now that's a cute idea. I prefer the conversion route, but it's great to give the OP (good!) alternatives.
T.J. Crowder
Very nice solution!
Tom Gullen
How do you switch the classes dynamically?
Tom Gullen
@Tom Gullen: You could add a class name like “metric-units”/‍“imperial-units” to `BODY` and change the mentioned CSS rules to `body.metric-units .metric {} body.metric-units .imperial { display: none }` and `body.imperial-units .metric { display: none } body.imperial-units .imperial { }`. Then you can toggle between the class names with `$(document.body).removeClass("imperial-units").addClass("metric-units")` and vice versa.
Gumbo