Can someone help me understand why var toAdd = $('input[name=checkListItem]').val()


#1

I did this correctly (or at least I'm under the impression I did as I passed the exercise), but I don't really understand the [ ] part of this code... what is this actually doing? Thanks!


#2
$(document).ready(function(){
$('#button').click(function(){

//// this part:

    var toAdd = $('input[name=checkListItem]').val();



    });
});

#3

Hi Justine,

It's a CSS attribute selector. In this case, we have something like this:

<input type="text" name="checkListItem">

Since there's no way by default to select that specific element using CSS, you can use a CSS attribute selector like so:

input[name="checkListItem"] {
  /* styles */
}

You can see more information here:
https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors


#4

Thank you for the explanation and the mdn link - it's very helpful!


#5

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.