JQuery Code Enquiry

Simon Leung
Simon Leung used Ask the Experts™
on
1. In the code below, what's "buttons.not(this)" means ? What does "this" refer to ?  Thx
2.  for the statement : if( !$("#formfield").val() ),  does formfield value return undefine if it is not set, and ! make the whole statement return true ?
3. What does $(function() means ?

Thx

<head>
    <link rel="stylesheet" href="day.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<button id="day" disabled>Day</button>
<button id="night">Night</button>

<script type="text/javascript">
    $(function() {
        var link = $('link[href="day.css"]'),
            buttons = $('button');
        buttons.on('click', function() {
            link.attr('href', this.id+'.css');
            buttons.not(this).removeAttr('disabled');
            $(this).attr('disabled', 'disabled');
        });
    });
</script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Team Lead
Commented:
1. In the code below, what's "buttons.not(this)" means ? What does "this" refer to ?

buttons refers to the object collection of "button". and since we called "buttons.not(this)" within "onclick" event, it refers to the buttons collection except for the button object that triggered the clicked event.

2.  for the statement : if( !$("#formfield").val() ),  does formfield value return undefine if it is not set, and ! make the whole statement return true ?

it means if the value of element with id=formfield is not blank

>>does formfield value return undefine if it is not set

not really, it will return blank as well.

This line: if( !$("#formfield").val() ) is not within your codes.

3. What does $(function() means ?

it's a self-invoke function, which means it will automatically started, without the need of being called.
leakim971Multitechnician
Top Expert 2014

Commented:
2. does formfield value return undefine if it is not set, and ! make the whole statement return true ?
it return an empty string and you right
! "" is true

3. What does $(function() means ?
$() is a shorthand of $(document).ready() // <--- click the link
so
$(function() { your multiline or not code here})  
is a shorthand of
$(document).ready(function() { your multiline or not code here})

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial