html - How can i check input field that has custom attribute with specific value using jQuery -
i have input fields custom attributes. want check if input field custom attribute has specific value alert match else not match.
(function ($) { $(".my_class").click(function () { var custom_attr = $(this).attr('custom_attr'); $("input").each(function () { var condation = $(this).attr('condation'); if (condation == custom_attr) { alert('match'); } else { alert('not match'); } }); }); })(jquery);
i think can try regex bases test like
if (!regexp.escape) { regexp.escape = function(value) { return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&") }; } (function($) { $(".my_class").click(function() { var custom_attr = $(this).attr('custom_attr'); var regex = new regexp(custom_attr.split(/\s+/).map(function(value) { return '(\\b' + regexp.escape(value) + '\\b)' }).join('|')); $('input').each(function() { var condation = $(this).attr('condation'); if (regex.test(condation)) { alert('match'); } else { alert('not match'); } }); }); })(jquery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <button type="button" name="abc" class="my_class" custom_attr="field1 field2">match</button> </div> <div> <input name="def" condation="field1 user_defined" /> <input name="jkl" condation="field2 user_defined" /> <input name="jkl" condation="field13 user_defined" /> </div>
Comments
Post a Comment