Skip to content

Commit 772f98b

Browse files
committed
Merge pull request #135 from RedWolves/FAQs
Added the 'How do I...' FAQs for issue #71
2 parents f15f942 + 59f24e8 commit 772f98b

12 files changed

+284
-0
lines changed

order.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@
8080
- faq:
8181
- add_keyboard_navigation
8282
- enable_the_back_button
83+
- how_do_i_select_an_item_using_class_or_id
84+
- how_do_I_select_elements_when_I_already_have_a_dom_element
85+
- how_do_i_test_whether_an_element_has_a_particular_class
86+
- how_do_i_test_whether_an_element_exists
87+
- how_do_i_determine_the_state_of_a_toggled_element
88+
- how_do_i_select_an_element_by_an_id_that_has_characters_used_in_css_notation
89+
- how_do_i_disable_enable_a_form_element
90+
- how_do_i_check_uncheck_a_checkbox_input_or_radio_button
91+
- how_do_i_get_the_text_value_of_a_selected_option
92+
- how_do_i_replace_text_from_the_3rd_element_of_a_list_of_10_items
93+
- how_do_i_pull_a_native_dom_element_from_a_jquery_object
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: How Do I Select Elements When I Already Have a DOM Element?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
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.
7+
8+
```
9+
var myDomElement = document.getElementById('foo'); // a plain DOM element
10+
$(myDomElement).find('a'); // finds all anchors inside the DOM element
11+
```
12+
13+
Many people try to concatenate a DOM element or jQuery object with a CSS selector, like so:
14+
15+
```
16+
$(myDomElement + '.bar'); // This is equivalent to $("[object HTMLElement].bar")
17+
```
18+
19+
Unfortunately, you cannot concatenate strings to objects.
20+
21+
###Related Articles
22+
23+
* [The jQuery Object](/using-jquery-core/jquery-object/)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: How do I check/uncheck a checkbox input or radio button?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
There are two ways to check/uncheck a checkbox/radio button.
7+
8+
Set the 'checked' attribute to true or false.
9+
10+
```
11+
// Check #x
12+
$('#x').attr('checked', true);
13+
// Uncheck #x
14+
$('#x').attr('checked', false);
15+
```
16+
17+
Add or remove the 'checked' attribute:
18+
19+
```
20+
// Check #x
21+
$("#x").attr('checked', 'checked');
22+
// Uncheck #x
23+
$("#x").removeAttr('checked');
24+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: How do I determine the state of a toggled element?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
You can determine whether an element is collapsed or not by using the :visible and :hidden selectors.
7+
8+
```
9+
var isVisible = $('#myDiv').is(':visible');
10+
var isHidden = $('#myDiv').is(':hidden');
11+
```
12+
13+
If you're simply acting on an element based on its visibility, just include ":visible" or ":hidden" in the selector expression. For example:
14+
15+
```
16+
$('#myDiv:visible').animate({left: '+=200px'}, 'slow');
17+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: How do I disable/enable a form element?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
There are two ways to disable/enable form elements.
7+
8+
Set the 'disabled' attribute to true or false:
9+
10+
```
11+
// Disable #x
12+
$('#x').attr('disabled', true);
13+
// Enable #x
14+
$('#x').attr('disabled', false);
15+
```
16+
17+
Add or remove the 'disabled' attribute:
18+
19+
```
20+
// Disable #x
21+
$("#x").attr('disabled', 'disabled');
22+
// Enable #x
23+
$("#x").removeAttr('disabled');
24+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: How do I get the text value of a selected option?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
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:
7+
8+
```
9+
$("#myselect").val();
10+
// => 1
11+
```
12+
13+
The second is the text value of the select. For example, using the following select box:
14+
15+
```
16+
<select id="myselect">
17+
<option value="1">Mr</option>
18+
<option value="2">Mrs</option>
19+
<option value="3">Ms</option>
20+
<option value="4">Dr</option>
21+
<option value="5">Prof</option>
22+
</select>
23+
```
24+
25+
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:
26+
27+
```
28+
$("#myselect option:selected").text();
29+
// => "Mr"
30+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: How do I pull a native DOM element from a jQuery object?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
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:
7+
8+
```
9+
$('#foo')[0]; // equivalent to document.getElementById('foo')
10+
```
11+
12+
The second method is to use the [get](http://api.jquery.com/get/) function:
13+
14+
```
15+
$('#foo').get(0); // identical to above, only slower
16+
```
17+
18+
You can also call [get](http://api.jquery.com/get/) without any arguments to retrieve a true array of DOM elements.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: How do I replace text from the 3rd element of a list of 10 items?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
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:
7+
8+
```
9+
// This doesn't work; text() returns a string, not the jQuery object
10+
$(this).find('li a').eq(2).text().replace('foo','bar');
11+
12+
// This works
13+
var $thirdLink = $(this).find('li a').eq(2);
14+
var linkText = $thirdLink.text().replace('foo','bar');
15+
$thirdLink.text(linkText);
16+
```
17+
18+
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.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: How do I select an element by an ID that has characters used in CSS notation?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
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.
7+
8+
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.
9+
10+
```
11+
// Does not work
12+
$("#some:id")
13+
14+
// Works!
15+
$("#some\\:id")
16+
// Does not work
17+
$("#some.id")
18+
19+
// Works!
20+
$("#some\\.id")
21+
```
22+
23+
The following function takes care of escaping these characters and places a "#" at the beginning of the ID string:
24+
25+
```
26+
function jq(myid) {
27+
return '#' + myid.replace(/(:|\.)/g,'\\$1');
28+
}
29+
```
30+
31+
The function can be used like so:
32+
33+
```
34+
$( jq('some.id') )
35+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: How Do I Select An Item Using Class or ID?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
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.
7+
8+
```
9+
$('#myDivId')
10+
```
11+
12+
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.
13+
14+
```
15+
$('.myCssClass')
16+
```
17+
18+
A jQuery object containing the selected element can be assigned to a JavaScript variable like normal:
19+
20+
```
21+
var myDivElement = $('#myDivId');
22+
```
23+
24+
Usually, elements in a jQuery object are acted on by other jQuery functions:
25+
26+
```
27+
var myValue = $('#myDivId').val(); // get the value of a form input
28+
29+
$('#myDivId').val("hello world"); // set the value of a form input
30+
```
31+
32+
### Related Articles
33+
34+
* [Selecting Elements](/using-jquery-core/selecting-elements/)
35+
* [Working with Selections](/using-jquery-core/working-with-selections/)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: How do I test whether an element exists?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
Use the [length](http://api.jquery.com/length/) property of the jQuery collection returned by your selector:
7+
8+
```
9+
if ( $('#myDiv').length )
10+
$('#myDiv').show();
11+
```
12+
13+
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:
14+
15+
```
16+
$('#myDiv').show();
17+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: How do I test whether an element has a particular class?
3+
source: http://docs.jquery.com/Frequently_Asked_Questions
4+
---
5+
6+
[hasClass](http://api.jquery.com/hasClass/) (added in version 1.2) handles this common use case:
7+
8+
```
9+
$("div").click(function(){
10+
if ( $(this).hasClass("protected") )
11+
$(this)
12+
.animate({ left: -10 })
13+
.animate({ left: 10 })
14+
.animate({ left: -10 })
15+
.animate({ left: 10 })
16+
.animate({ left: 0 });
17+
});
18+
```
19+
20+
You can also use the [is()](http://api.jquery.com/is/) method along with an appropriate selector for more advanced matching:
21+
22+
```
23+
if ( $('#myDiv').is('.pretty.awesome') )
24+
$('#myDiv').show();
25+
```
26+
27+
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):
28+
29+
```
30+
if ( $('#myDiv').is(':hidden') )
31+
$('#myDiv').show();
32+
```

0 commit comments

Comments
 (0)