Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Last active December 18, 2015 02:39
Show Gist options
  • Save horitaku1124/5712235 to your computer and use it in GitHub Desktop.
Save horitaku1124/5712235 to your computer and use it in GitHub Desktop.
DOMを操作するときのjQueryとかのやりかた

チェックボックスがチェックされているか。

DOM
document.getElementById('id').checked
jQuery
$('#id').is(":checked");

チェックボックスを設定

DOM
document.getElementById('id').checked = true
jQuery
$('#id').attr("checked", true);
より
$('#id').prop("checked", true);

プルダウンの選択されている値を取得

DOM
var i = document.getElementById('id').selectedIndex;
var value = document.getElementById('id').options[i].value;
jQuery
$('#id').val()

プルダウンの選択されている表示名

jQuery
$('#id').children(':selected').html()

ラジオボタンに値を設定する

DOM
document.getElementsByName("name")[1].checked = true
jQuery
$('#form_' + id).val([val]);

ラジオボタンで選択されている値

jQuery

$('input[name="kind"]:checked').val()

DOM

Array.apply(null, document.getElementsByName("name")).reduce(function(r, e){
  return e.checked ? e.value : (typeof r == "string" ? r : (r && r.checked ? r.value : null));
}) // => string or null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment