diff --git a/order.yml b/order.yml index 286b3481..3ed1b59f 100644 --- a/order.yml +++ b/order.yml @@ -80,3 +80,14 @@ - faq: - add_keyboard_navigation - enable_the_back_button + - how_do_i_select_an_item_using_class_or_id + - how_do_I_select_elements_when_I_already_have_a_dom_element + - how_do_i_test_whether_an_element_has_a_particular_class + - how_do_i_test_whether_an_element_exists + - how_do_i_determine_the_state_of_a_toggled_element + - how_do_i_select_an_element_by_an_id_that_has_characters_used_in_css_notation + - how_do_i_disable_enable_a_form_element + - how_do_i_check_uncheck_a_checkbox_input_or_radio_button + - how_do_i_get_the_text_value_of_a_selected_option + - how_do_i_replace_text_from_the_3rd_element_of_a_list_of_10_items + - how_do_i_pull_a_native_dom_element_from_a_jquery_object diff --git a/page/faq/how_do_I_select_elements_when_I_already_have_a_dom_element.md b/page/faq/how_do_I_select_elements_when_I_already_have_a_dom_element.md new file mode 100644 index 00000000..e17fe481 --- /dev/null +++ b/page/faq/how_do_I_select_elements_when_I_already_have_a_dom_element.md @@ -0,0 +1,23 @@ +--- +title: How Do I Select Elements When I Already Have a DOM Element? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object. + +``` + var myDomElement = document.getElementById('foo'); // a plain DOM element + $(myDomElement).find('a'); // finds all anchors inside the DOM element +``` + +Many people try to concatenate a DOM element or jQuery object with a CSS selector, like so: + +``` +$(myDomElement + '.bar'); // This is equivalent to $("[object HTMLElement].bar") +``` + +Unfortunately, you cannot concatenate strings to objects. + +###Related Articles + +* [The jQuery Object](/using-jquery-core/jquery-object/) \ No newline at end of file diff --git a/page/faq/how_do_i_check_uncheck_a_checkbox_input_or_radio_button.md b/page/faq/how_do_i_check_uncheck_a_checkbox_input_or_radio_button.md new file mode 100644 index 00000000..774d051c --- /dev/null +++ b/page/faq/how_do_i_check_uncheck_a_checkbox_input_or_radio_button.md @@ -0,0 +1,24 @@ +--- +title: How do I check/uncheck a checkbox input or radio button? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +There are two ways to check/uncheck a checkbox/radio button. + +Set the 'checked' attribute to true or false. + +``` + // Check #x + $('#x').attr('checked', true); + // Uncheck #x + $('#x').attr('checked', false); +``` + +Add or remove the 'checked' attribute: + +``` + // Check #x + $("#x").attr('checked', 'checked'); + // Uncheck #x + $("#x").removeAttr('checked'); +``` \ No newline at end of file diff --git a/page/faq/how_do_i_determine_the_state_of_a_toggled_element.md b/page/faq/how_do_i_determine_the_state_of_a_toggled_element.md new file mode 100644 index 00000000..8e0c179c --- /dev/null +++ b/page/faq/how_do_i_determine_the_state_of_a_toggled_element.md @@ -0,0 +1,17 @@ +--- +title: How do I determine the state of a toggled element? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +You can determine whether an element is collapsed or not by using the :visible and :hidden selectors. + +``` + var isVisible = $('#myDiv').is(':visible'); + var isHidden = $('#myDiv').is(':hidden'); +``` + +If you're simply acting on an element based on its visibility, just include ":visible" or ":hidden" in the selector expression. For example: + +``` + $('#myDiv:visible').animate({left: '+=200px'}, 'slow'); +``` \ No newline at end of file diff --git a/page/faq/how_do_i_disable_enable_a_form_element.md b/page/faq/how_do_i_disable_enable_a_form_element.md new file mode 100644 index 00000000..f1a398a3 --- /dev/null +++ b/page/faq/how_do_i_disable_enable_a_form_element.md @@ -0,0 +1,24 @@ +--- +title: How do I disable/enable a form element? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +There are two ways to disable/enable form elements. + +Set the 'disabled' attribute to true or false: + +``` + // Disable #x + $('#x').attr('disabled', true); + // Enable #x + $('#x').attr('disabled', false); +``` + +Add or remove the 'disabled' attribute: + +``` + // Disable #x + $("#x").attr('disabled', 'disabled'); + // Enable #x + $("#x").removeAttr('disabled'); +``` \ No newline at end of file diff --git a/page/faq/how_do_i_get_the_text_value_of_a_selected_option.md b/page/faq/how_do_i_get_the_text_value_of_a_selected_option.md new file mode 100644 index 00000000..fe22b089 --- /dev/null +++ b/page/faq/how_do_i_get_the_text_value_of_a_selected_option.md @@ -0,0 +1,30 @@ +--- +title: How do I get the text value of a selected option? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +Select elements typically have two values that you want to access. First there's the value to be sent to the server, which is easy: + +``` + $("#myselect").val(); + // => 1 +``` + +The second is the text value of the select. For example, using the following select box: + +``` + +``` + +If you wanted to get the string "Mr" if the first option was selected (instead of just "1"), you would do that in the following way: + +``` + $("#myselect option:selected").text(); + // => "Mr" +``` \ No newline at end of file diff --git a/page/faq/how_do_i_pull_a_native_dom_element_from_a_jquery_object.md b/page/faq/how_do_i_pull_a_native_dom_element_from_a_jquery_object.md new file mode 100644 index 00000000..ad2a5cdc --- /dev/null +++ b/page/faq/how_do_i_pull_a_native_dom_element_from_a_jquery_object.md @@ -0,0 +1,18 @@ +--- +title: How do I pull a native DOM element from a jQuery object? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +A jQuery object is an array-like wrapper around one or more DOM elements. To get a reference to the actual DOM elements (instead of the jQuery object), you have two options. The first (and fastest) method is to use array notation: + +``` + $('#foo')[0]; // equivalent to document.getElementById('foo') +``` + +The second method is to use the [get](http://api.jquery.com/get/) function: + +``` + $('#foo').get(0); // identical to above, only slower +``` + +You can also call [get](http://api.jquery.com/get/) without any arguments to retrieve a true array of DOM elements. \ No newline at end of file diff --git a/page/faq/how_do_i_replace_text_from_the_3rd_element_of_a_list_of_10_items.md b/page/faq/how_do_i_replace_text_from_the_3rd_element_of_a_list_of_10_items.md new file mode 100644 index 00000000..ae9f75c9 --- /dev/null +++ b/page/faq/how_do_i_replace_text_from_the_3rd_element_of_a_list_of_10_items.md @@ -0,0 +1,18 @@ +--- +title: How do I replace text from the 3rd element of a list of 10 items? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +Either the :eq() selector or the .eq() method will allow you to select the proper item. However, to replace the text, you must get the value before you set it: + +``` + // This doesn't work; text() returns a string, not the jQuery object + $(this).find('li a').eq(2).text().replace('foo','bar'); + + // This works + var $thirdLink = $(this).find('li a').eq(2); + var linkText = $thirdLink.text().replace('foo','bar'); + $thirdLink.text(linkText); +``` + +The first example just discards the modified text. The second example saves the modified text and then replaces the old text with the new modified text. Remember, .text() gets; .text("foo") sets. \ No newline at end of file diff --git a/page/faq/how_do_i_select_an_element_by_an_id_that_has_characters_used_in_css_notation.md b/page/faq/how_do_i_select_an_element_by_an_id_that_has_characters_used_in_css_notation.md new file mode 100644 index 00000000..2813e512 --- /dev/null +++ b/page/faq/how_do_i_select_an_element_by_an_id_that_has_characters_used_in_css_notation.md @@ -0,0 +1,35 @@ +--- +title: How do I select an element by an ID that has characters used in CSS notation? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +Because jQuery uses CSS syntax for selecting elements, some characters are interpreted as CSS notation. For example, ID attributes, after an initial letter (a-z or A-Z), may also use periods and colons, in addition to letters, numbers, hyphens, and underscores (see [W3C Basic HTML Data Types](http://www.w3.org/TR/html4/types.html#type-id)). The colon (":") and period (".") are problematic within the context of a jQuery selector because they indicate a pseudo-class and class, respectively. + +In order to tell jQuery to treat these characters literally rather than as CSS notation, they must be "escaped" by placing two backslashes in front of them. + +``` + // Does not work + $("#some:id") + + // Works! + $("#some\\:id") + // Does not work + $("#some.id") + + // Works! + $("#some\\.id") +``` + +The following function takes care of escaping these characters and places a "#" at the beginning of the ID string: + +``` + function jq(myid) { + return '#' + myid.replace(/(:|\.)/g,'\\$1'); + } +``` + +The function can be used like so: + +``` + $( jq('some.id') ) +``` \ No newline at end of file diff --git a/page/faq/how_do_i_select_an_item_using_class_or_id.md b/page/faq/how_do_i_select_an_item_using_class_or_id.md new file mode 100644 index 00000000..d396c906 --- /dev/null +++ b/page/faq/how_do_i_select_an_item_using_class_or_id.md @@ -0,0 +1,35 @@ +--- +title: How Do I Select An Item Using Class or ID? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +This code selects an element with an ID of "myDivId". Since IDs are unique, this expression always selects either zero or one elements depending upon whether or not an element with the specified ID exists. + +``` +$('#myDivId') +``` + +This code selects an element with a class of "myCssClass". Since any number of elements can have the same class, this expression will select any number of elements. + +``` +$('.myCssClass') +``` + +A jQuery object containing the selected element can be assigned to a JavaScript variable like normal: + +``` +var myDivElement = $('#myDivId'); +``` + +Usually, elements in a jQuery object are acted on by other jQuery functions: + +``` +var myValue = $('#myDivId').val(); // get the value of a form input + +$('#myDivId').val("hello world"); // set the value of a form input +``` + +### Related Articles + +* [Selecting Elements](/using-jquery-core/selecting-elements/) +* [Working with Selections](/using-jquery-core/working-with-selections/) \ No newline at end of file diff --git a/page/faq/how_do_i_test_whether_an_element_exists.md b/page/faq/how_do_i_test_whether_an_element_exists.md new file mode 100644 index 00000000..1032edb7 --- /dev/null +++ b/page/faq/how_do_i_test_whether_an_element_exists.md @@ -0,0 +1,17 @@ +--- +title: How do I test whether an element exists? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +Use the [length](http://api.jquery.com/length/) property of the jQuery collection returned by your selector: + +``` +if ( $('#myDiv').length ) + $('#myDiv').show(); +``` + +Note that it isn't always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no errors) if it does not: + +``` + $('#myDiv').show(); +``` \ No newline at end of file diff --git a/page/faq/how_do_i_test_whether_an_element_has_a_particular_class.md b/page/faq/how_do_i_test_whether_an_element_has_a_particular_class.md new file mode 100644 index 00000000..aaceacaa --- /dev/null +++ b/page/faq/how_do_i_test_whether_an_element_has_a_particular_class.md @@ -0,0 +1,32 @@ +--- +title: How do I test whether an element has a particular class? +source: http://docs.jquery.com/Frequently_Asked_Questions +--- + +[hasClass](http://api.jquery.com/hasClass/) (added in version 1.2) handles this common use case: + +``` +$("div").click(function(){ + if ( $(this).hasClass("protected") ) + $(this) + .animate({ left: -10 }) + .animate({ left: 10 }) + .animate({ left: -10 }) + .animate({ left: 10 }) + .animate({ left: 0 }); +}); +``` + +You can also use the [is()](http://api.jquery.com/is/) method along with an appropriate selector for more advanced matching: + +``` +if ( $('#myDiv').is('.pretty.awesome') ) + $('#myDiv').show(); +``` + +Note that this method allows you to test for other things as well. For example, you can test whether an element is hidden (by using the custom [:hidden](http://api.jquery.com/hidden-selector/) selector): + +``` +if ( $('#myDiv').is(':hidden') ) + $('#myDiv').show(); +``` \ No newline at end of file