javascript - Trigger by suffix from same element class -
i want make every button have suffix class -dummy
(let's call "dummy") trigger button have same class without suffix (call "real button").
<button class="btn-submit hidden">
triggered <button class="btn-submit-dummy">
<button class="btn-edit hidden">
triggered <button class="btn-edit-dummy">
<button class="btn-delete">
triggered nothing since there no element class btn-delete-dummy
each real button need triggered click on dummy.
my script far :
/** if define manually on each button **/ $(".btn-submit-dummy").click(function() { $(".btn-submit").trigger("click"); }); $(".btn-edit-dummy").click(function() { $(".btn-edit").trigger("click"); }); ... ... ... /** end of defining button **/
my idea if written in jquery , human language (sorry ugly way) following :
$("button[class$='-dummy']").click(function() { var realbutton = button_which_have_same_class_as_clicked_object_but_trimmed_with_"-dummy"; if(find realbutton) { $(realbutton).trigger("click"); } });
please help. lot.
try this:
$("[class$='-dummy']").on('click', function () { var btnclass = $(this).attr('class').match(/([\w-]+-dummy)+/)[0].replace('-dummy', ''); alert(btnclass); $("." + btnclass).trigger("click"); });
demo: https://jsfiddle.net/tusharj/gc3d5yy5/
hope helps
Comments
Post a Comment