Adding a table row in jQuery

The approach you suggest is not guaranteed to give you the result you’re looking for – what if you had a tbody for example: <table id=”myTable”> <tbody> <tr>…</tr> <tr>…</tr> </tbody> </table> You would end up with the following: <table id=”myTable”> <tbody> <tr>…</tr> <tr>…</tr> </tbody> <tr>…</tr> </table> I would therefore recommend this approach instead: $(‘#myTable tr:last’).after(‘<tr>…</tr><tr>…</tr>’); … Read more

Scroll to an element with jQuery

Assuming you have a button with the id button, try this example: $(“#button”).click(function() { $([document.documentElement, document.body]).animate({ scrollTop: $(“#elementtoScrollToID”).offset().top }, 2000); }); I got the code from the article Smoothly scroll to an element without a jQuery plugin. And I have tested it on the example below. <html> <script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js”></script> <script> $(document).ready(function (){ $(“#click”).click(function (){ $(‘html, … Read more

How do I refresh a page using JavaScript?

Use location.reload(). For example, to reload whenever an element with id=”something” is clicked: $(‘#something’).click(function() { location.reload(); }); The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache. The parameter defaults to false, so by default the page may reload from the … Read more

How can I know which radio button is selected via jQuery?

To get the value of the selected radioName item of a form with id myForm: $(‘input[name=radioName]:checked’, ‘#myForm’).val() Here’s an example: $(‘#myForm input’).on(‘change’, function() { alert($(‘input[name=radioName]:checked’, ‘#myForm’).val()); }); <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <form id=”myForm”> <fieldset> <legend>Choose radioName</legend> <label><input type=”radio” name=”radioName” value=”1″ /> 1</label> <br /> <label><input type=”radio” name=”radioName” value=”2″ /> 2</label> <br /> <label><input type=”radio” name=”radioName” value=”3″ /> … Read more

Why does my JavaScript code receive a “No ‘Access-Control-Allow-Origin’ header is present on the requested resource” error, while Postman does not?

If I understood it right you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request. A tutorial about … Read more

event.preventDefault() vs. return false

return false from within a jQuery event handler is effectively the same as calling both e.preventDefault and e.stopPropagation on the passed jQuery.Event object. e.preventDefault() will prevent the default event from occuring, e.stopPropagation() will prevent the event from bubbling up and return false will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, … Read more