Posts Tagged JavaScript
Change text box value in uppercase using jQuery
Posted by admin in Beginners, HTML, JavaScript, jQuery on September 27th, 2011
We can bind this functionality with text box KeyUp event. So, when type anything this code will change the text box value in uppercase with each keystroke.
Code Snippet:-
<input type="text" id="variable" name="variable" />
<script type="text/javascript"> $(document).ready(function() { $('#variable').keyup(function() { $(this).val($(this).val().toUpperCase()); }); }); </script>
jQuery get selected text from SELECT (or DROPDOWN) list box
Posted by admin in Beginners, JavaScript, jQuery on September 8th, 2011
Most of the time in JavaScript we want to do following things with Select (or dropdown) list box.
- Get the value of selected option
- Get the text of selected option
- Get the text of option using its value
We can use jQuery for all of the above tasks. jQuery provide very simple one line solution for all of them. Find below code snippet of all three one by one.
Code Snippet:
// Select list box <select id="dropdown"> <option value="0">Sunday</option> <option value="1">Monday</option> <option value="2">Tuesday</option> <option value="3" selected="selected">Wednesday</option> <option value="4">Thursday</option> <option value="5">Friday</option> <option value="6">Saturday</option> </select> // Get the value of selected option var selectedvalue = $('#dropdown').val(); alert(selectedvalue); // 3 // Get the text of selected option var selectedText = $('#dropdown option:selected').text(); alert(selectedText); // Wednesday // Get the text of option using its option value (eg: 5) var textThroughValue = $("#dropdown option[value='5']").text(); alert(textThroughValue); // Friday
How know Base Path of Drupal in JavaScript
Posted by admin in Drupal, JavaScript, jQuery on May 17th, 2010
Sometimes you want to know about the Base Path of your Drupal installation in JavaScript. You may want to know about this path for sending the AJAX request at correct URL relatively, load the image or some other type of file on client side through JavaScript etc.
Drupal provide a variable called Drupal.settings.basePath for this purpose in JavaScript. But this variable is only available if you include any custom JavaScript file or JavaScript block in your module. By default Drupal doesn’t include the ‘jquery.js‘ and ‘drupal.js‘ file in generated output. It only add these files if you add your custom JavaScript file or JavaScript block. We can include our custom JavaScript file in Drupal like this:-
drupal_add_js(drupal_get_path('module','my_module'). '/my_module.js');
The my_module.js file should reside in same directory where my_module module exist. This will force Drupal to add jquery.js and drupal.js files in output.
These two files initialize the Base Path variable. In drupal.js file, on top you can see the initialization of setting object in Drupal object. Like this:-
var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'themes': {}, 'locale': {} };
Drupal generate some JavaScript code in Script block inside head tag of each page for extending the settings object with the help of jQuery. Like this:-
jQuery.extend(Drupal.settings, { "basePath": "/" });
You can test the Base Path by adding following code in my_module.js file.
$(document).ready(function() { alert(Drupal.settings.basePath); });
This will alert the Base Path for Drupal installation after loading of the page.
Round Number upto 2 decimal places in JavaScript
Posted by admin in JavaScript on January 17th, 2010
JavaScript’s Math.round() function doesn’t provide facility to round off a number up to 2 or more decimal places. So, here I’m showing a simple JavaScript code using which you can round off numbers up to 2 decimal places. You can change this for more decimal places.
var retailPrice = 9.99; var percentage = 0.2525; /* 25.25% */ var decimalPlace = 100; /* 100 for 2 decimal place. 1000 for 3 decimal place.*/ var wholeSalePrice = Math.round(percentage * retailPrice * decimalPlace) / decimalPlace; alert(wholeSalePrice); /* This will show 2.52 */
