0

I am supposed to use Jquery to call a function that changes a div's visibility using css. This is my JQUERY

    alert("Interactive on line");
    function changeView(page)
        {
              alert("Handler for click() called. ");
              if(page === 'home)
                 {
                    $('#HomeTab'.css(display=='none'));
                 }
        }
    $('#HomeTab').on('click', { page:'home'}, changeView());

I used the alert statement inside the changeView to see if changeView ever gets called and it does not. The initial alert before the changeView function does get called, so the script is linked correctly. Thank you in advance!

1
  • I copied Rayon's snippet word for word and pasted it in my folder. It still does not work :/. The outer alert is being called, but it does not even get to the inner alert. Commented Jun 23, 2016 at 4:11

1 Answer 1

4

Refer .on( events [, selector ] [, data ], handler )

(a) You are invoking/calling function, not assigning it as handler, Remove () after function name.

(b) data object could be accessed using event.data.KEY

(c) Also correct the typo while using .css method, it is jQueryElement.css(PROPERTY,VALUE)

function changeView(e) {
  alert("Handler for click() called. ");
  if (e.data.page === 'home') {
    $('#HomeTab').css('display', 'none');
    //OR $('#HomeTab').hide();
  }
}
$('#HomeTab').on('click', {
  page: 'home'
}, changeView);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id='HomeTab'>Home Tab</div>

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

2 Comments

i suggest to just add a class with the desired style rather than directly adding css using jquery :)
@guradio, It depends on developers preference.. OP has few syntax, in his code..Suggesting something else would not make it good answer. I do not find any harm in using .css method but again, It is developers preference I would have opted for class approach if I had to change many styles than just one..But yes, OP could go with jQelem.hide()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.