Last active
December 27, 2015 11:09
-
-
Save YoshitsuguFujii/7315972 to your computer and use it in GitHub Desktop.
extend_jquery.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ -> | |
jQuery.extend jQuery.fn, | |
toggle_value: -> | |
obj = jQuery(this) | |
obj.val ((if obj.val() is "true" then "false" else "true")) | |
exists: -> | |
@length > 0 | |
enable: -> | |
$(this).removeAttr "disabled" | |
disabled: -> | |
$(this).attr "disabled", "disabled" | |
member: (str) -> | |
$(this).html().indexOf(str) isnt -1 | |
checked: (str) -> | |
$(this).attr "checked" | |
check: (str) -> | |
$(this).attr "checked", "checked" | |
uncheck: (str) -> | |
$(this).removeAttr "checked" | |
only_one_inputable: -> | |
$(@).bind "change", -> | |
entered_input = null | |
$(@).find("input").each -> | |
$(@).disabled() | |
if $(@).val() != '' | |
entered_input = $(@) | |
if entered_input | |
entered_input.enable() | |
else | |
$(@).find("input").each -> | |
$(@).enable() | |
clear_input: -> | |
$(@).find("textarea, :text, select").val("").end().find(":checked").prop("checked", false) | |
clear_text_input: -> | |
$(@).find("textarea, :text").val("").end() | |
check_radio_first: -> | |
$(@).find("input:radio:first").prop("checked", true) | |
exists_text_val: -> | |
vals = _.map $(@).find("textarea, :text"), (elem) -> | |
return $(elem).val() | |
console.log(vals) | |
not _.isEmpty(_.compact(vals)) | |
serializeHash: -> | |
attrs = {} | |
_.each $(@).serializeArray(), (field) -> | |
attrs[field.name] = field.value | |
attrs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment