0

I have a small jquery script designed to strip an element of a class. My site is running on wordpress however I am getting errors every time I try the window resize handler is called.

jQuery(document).ready(

    function watchSize($) {
    var $windowSize = $(window).width();
    var $target = ('#WoodsworthResidenceLogo');

    if ($windowSize < 960 && $target.hasClass('span_2')) {
        $target.removeClass('span_2');
    }

    $(window).resize(
        function() {
        var $windowSize = $(window).width();
        var $target = ('#myelementID');

    if ($windowSize < 960 && $target.hasClass('span_2')) {
        $target.removeClass('span_2');
        } else if ($windowSize > 960 && $target.hasClass('span_2')){
            $target.addClass('span_2');
        }
    });
});

It seems most of my error comes from the $target variable. Any ideas or suggestions, I'm very new to javascript and jquery. Any help would be appreciated.

EDIT:

I'm also unsure of the scope of the variables, thus I have them redeclared in the nested function.

1 Answer 1

2

You have a syntax mistake using the jquery library. Your current code is simply assigning a string to $target. It would be the same as doing

var $target = '#myelementID'; 

You have to replace

var $target = ('#WoodsworthResidenceLogo');

and

var $target = ('#myelementID'); 

by

var $target = $('#WoodsworthResidenceLogo');

and

var $target = $('#myelementID');

Notice the $ in front of the (..). This is an alias for jQuery

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

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.