Mr Sorbose
Mr Sorbose

Reputation: 777

Javascript update causing grief

I am not a javascript developer by any means what so ever, however am stuck behind a JS problem.

If somebody could help me figure out what the problem may be would be amazing.

The bit of script that is falling over is:

        $('button').each(function()
            {
            var curr_title = $(this).attr('title');

            if(curr_title.length > 0)   $(this).button({ icons: { primary: curr_title } });
            else                        $(this).button();
            });

The error is

TypeError: Cannot read property 'length' of undefined.

This works no problems at all in previous releases of the code. however the only change is updated Jquery.

Does anyone know if this is likely to be the problem?

Again. Apologies if im being stupid.

Upvotes: 1

Views: 67

Answers (3)

Sirko
Sirko

Reputation: 74076

The value returned in this line is undefined (probably because the button it refers to has no such attribute specified.)

var curr_title = $(this).attr('title');

jQuery changed the behavior of attr() in version 1.6

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set.

So the call in the if statement fails. To "repair" this, you could just add a check for undefined like this:

if( (typeof curr_title != 'undefined') && (curr_title.length > 0)) {
   $(this).button({ icons: { primary: curr_title } });
} else {
   $(this).button();
}

Upvotes: 2

Liam
Liam

Reputation: 29714

$(this).attr('title'); is undefined, so has no length. does your button contain a title, if your testing for wether the attibute exists try:

$('button').each(function()
            {
            var curr_title = $(this).attr('title');

            if(curr_title != undefined)  
                $(this).button({ icons: { primary: curr_title } });
            else                        
                $(this).button();
            });

Upvotes: 0

Mike Simmons
Mike Simmons

Reputation: 1298

looks like you might have a button in the markup without a "title" attribute. For this button (or buttons) curr_title is undefined, so calling .length on it raises the exception

Upvotes: 0

Related Questions