Skip to content

Added the 'How do I...' FAQs for issue #71 #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions order.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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/)
Original file line number Diff line number Diff line change
@@ -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');
```
17 changes: 17 additions & 0 deletions page/faq/how_do_i_determine_the_state_of_a_toggled_element.md
Original file line number Diff line number Diff line change
@@ -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');
```
24 changes: 24 additions & 0 deletions page/faq/how_do_i_disable_enable_a_form_element.md
Original file line number Diff line number Diff line change
@@ -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');
```
30 changes: 30 additions & 0 deletions page/faq/how_do_i_get_the_text_value_of_a_selected_option.md
Original file line number Diff line number Diff line change
@@ -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:

```
<select id="myselect">
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>
<option value="4">Dr</option>
<option value="5">Prof</option>
</select>
```

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"
```
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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') )
```
35 changes: 35 additions & 0 deletions page/faq/how_do_i_select_an_item_using_class_or_id.md
Original file line number Diff line number Diff line change
@@ -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/)
17 changes: 17 additions & 0 deletions page/faq/how_do_i_test_whether_an_element_exists.md
Original file line number Diff line number Diff line change
@@ -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();
```
Original file line number Diff line number Diff line change
@@ -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();
```