Category : JavaScript

Javascript get elements by name

Javascript get elemens by name: <html> <head> <script type=”text/javascript”> function getInputValues(varName) { varValues = document.getElementsByName(varName); for (var i = 0; i < varValues.length; i++) { alert(varValues[i].value); } } </script> </head> <body> <input type=”text” name=”xName” ><br /> <input type=”text” name=”xName” ><br /> <input type=”text” name=”xName” ><br /> <input type=”button” onclick=”getInputValues(‘xName’); return false;” value=”Display Values”> </body> </html>

Javascript – confirm leave the page

Javascript code to be used when you need a confirmation before leaving a page: <script language=”javascript” type=”text/javascript”> window.onbeforeunload = confirmClose; function confirmClose(){ return “Are you sure do you want to leave this page.”; } </script>

Javascript – convert type

Convert variable to boolean var booleanValue = Boolean(variableValue); Converting to String var stringValue = String(variableValue); Converting to Number var numberValue = Number(variableValue); Parsing to Number Parse to float value parseFloat(variableValue) Parse to Integer Value parseInt(variableValue)

JQuery – Ajax request with jQuery

Perform an Ajax request with jQuery: jQuery.ajax({ url: ‘url_of_ajax_request’, success: function(response) { alert(‘Response from url is: ‘ + response) } }); Source: JQuery Ajax

Javascript confirm window

Next code can be used to add a confirm window from javascript: var conf = confirm(“Are you sure do you want to delete?”); if (conf == true) { // Code to execute }

1 2 3