How to format a phone number with jQuery

Simple: http://jsfiddle.net/Xxk3F/3/ $(‘.phone’).text(function(i, text) { return text.replace(/(\d{3})(\d{3})(\d{4})/, ‘$1-$2-$3’); }); Or: http://jsfiddle.net/Xxk3F/1/ $(‘.phone’).text(function(i, text) { return text.replace(/(\d\d\d)(\d\d\d)(\d\d\d\d)/, ‘$1-$2-$3’); }); Note: The .text() method cannot be used on input elements. For input field text, use the .val() method.

Get the contents of a table row with a button click

The object of the exercise is to find the row that contains the information. When we get there, we can easily extract the required information. Answer $(“.use-address”).click(function() { var $item = $(this).closest(“tr”) // Finds the closest row <tr> .find(“.nr”) // Gets a descendent with class=”nr” .text(); // Retrieves the text within <td> $(“#resultas”).append($item); // Outputs …

Read more

jQuery each loop in table row [duplicate]

In jQuery just use: $(‘#tblOne > tbody > tr’).each(function() {…code…}); Using the children selector (>) you will walk over all the children (and not all descendents), example with three rows: $(‘table > tbody > tr’).each(function(index, tr) { console.log(index); console.log(tr); }); Result: 0 <tr> 1 <tr> 2 <tr> In VanillaJS you can use document.querySelectorAll() and walk …

Read more

jQuery UI accordion that keeps multiple sections open?

Pretty simple: <script type=”text/javascript”> (function($) { $(function() { $(“#accordion > div”).accordion({ header: “h3″, collapsible: true }); }) })(jQuery); </script> <div id=”accordion”> <div> <h3><a href=”#”>First</a></h3> <div>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div> </div> <div> <h3><a href=”#”>Second</a></h3> <div>Phasellus mattis tincidunt nibh.</div> </div> <div> <h3><a href=”#”>Third</a></h3> <div>Nam dui erat, auctor …

Read more