javascript - I'm `keyup` listening to elements of a class, but I want to get an attribute from the field that is "keyup'ed" -
in rails app listening on bunch of input fields class regex-field
. listening coffeescript code. every time typing 1 of input fields, resetregex
method triggered. can see below want value of regexfield.attr "regex"
. seems same value comes out regardless of field i'm typing into. idea i'm doing wrong?
$ -> createsubtable(data, email_type_id) resetregex() $(document).ajaxsuccess (event, request, settings) -> resetregex() resetregex = -> regexfield = $(".regex-field") regexfield.off "keyup" regexfield.keyup (event) -> email_type = regexfield.attr "email-type-id" console.log regexfield # => [input#order-id-regex.regex-field, input#name-regex.regex-field, input#mail-address-regex.regex-field, prevobject: jquery.fn.jquery.init[1], context: document, selector: ".regex-field", jquery: "1.9.1", constructor: function…] console.log regexfield.attr "regex" # => order_id_regex $.ajax type: "get" datatype: "json" data: value: regexfield[0].value database_field: regexfield.attr "regex" url: "/api/email_type/"+email_type+"/save_regex" success: (data, status, xhr) -> error: (xhr, textstatus, errorthrown) -> createsubtable = (data, email_type_id) -> jobscontainer = $("#jobs") jobstable2 = $("<table />").addclass("table") jobscontainer.append(jobstable2) jobsthead2 = $("<thead />") jobsheadtr2 = $("<tr />") jobsth2 = $("<th />").text("email body") jobsheadtr2.append(jobsth2) jobsthead2.append(jobsheadtr2) jobstable2.append(jobsthead2) jobstbody2 = $("<tbody />") linenumber = 0 data[0].extra.test_return.body.map (line) -> jobsbodytrloop = $("<tr />") jobstdloop4 = $("<td />") if data[0].extra.chosen_fields.order_id_line linenumber typeinput1 = $("<input />").addclass("regex-field").attr("email-type-id", email_type_id).attr("id", "order-id-regex").attr("regex", "order_id_regex") jobstdloop4.append(typeinput1) else if data[0].extra.chosen_fields.mail_address_line linenumber typeinput1 = $("<input />").addclass("regex-field").attr("email-type-id", email_type_id).attr("id", "mail-address-regex").attr("regex", "mail_address_regex") jobstdloop4.append(typeinput1) else if data[0].extra.chosen_fields.name_line linenumber typeinput1 = $("<input />").addclass("regex-field").attr("email-type-id", email_type_id).attr("id", "name-regex").attr("regex", "name_regex") jobstdloop4.append(typeinput1) jobsbodytrloop.append(jobstdloop4) jobstbody2.append(jobsbodytrloop) linenumber++ jobstable2.append(jobstbody2)
you need use $(this)
reference element triggered event.
regexfield.keyup (event) -> email_type = $(this).attr "email-type-id" console.log $(this) # => should output field triggered event console.log $(this).attr "regex" # => attr "regex" of field
Comments
Post a Comment