Found 9326 Articles for Object Oriented Programming

What is the best JavaScript code to create an img element?

Ankith Reddy
Updated on 23-Jun-2020 12:07:26

115 Views

Try any of the following for the width of the image you want to create −myImg.setAttribute('width', '5px');OrmyImg.width = '5';OrmyImg.style.width = '5px';You can also try the following code to add the width and height to the background image −var myImg = new Image(5,5); myImg.src = 'http://www.example.com';

How to create JavaScript regexes using string variables?

Abhinaya
Updated on 23-Jun-2020 12:07:53

91 Views

Yes, use new RegExp(pattern, flags) to achieve this in JavaScript.You can try to run the following code to implement JavaScript regex using string variables −                    var str = 'HelloWorld'.replace( new RegExp('hello', 'i'), '' );          document.write(str);          

Is their JavaScript “not in” operator for checking object properties?

Vrundesha Joshi
Updated on 23-Jun-2020 11:59:16

1K+ Views

To get only the else part, just negate, using ! the operator in JavaScript. You can try to run the following code to achieve this −var arr = {}; if (!(id in arr)) { } else { }In addition, if you want the existence of a property in an object, then use hasOwnProperty −if (!arr.hasOwnProperty(id)) { } else { }

Create a syntax highlighting code with JavaScript.

Srinivas Gorla
Updated on 23-Jun-2020 12:00:35

112 Views

To create a syntax-highlighting code with JavaScript, use the prettify library. Add the following in your code to add the library for syntax highlighting −To use it, add the following to your tag −    Code comes here

How to create a JavaScript code for multiple keys pressed at once?

Arjun Thakur
Updated on 23-Jun-2020 11:59:51

491 Views

Use the keydown event in JavaScript to get to know which keys are pressed at once. The following is the script −Examplevar log = $('#log')[0],    keyPressed = []; $(document.body).keydown(function (evt) {    var li = keyPressed [evt.keyCode];    if (!li) {       li = log.appendChild(document.createElement('li'));       keyPressed [evt.keyCode] = li;    }    $(li).text(Key Down: ' + evt.keyCode);    $(li).removeClass('key-up'); }); $(document.body).keyup(function (evt) {    var li = keyPressed [evt.keyCode];    if (!li) {       li = log.appendChild(document.createElement('li'));    }    $(li).text('Key Up: ' + evt.keyCode);    $(li).addClass('key-up'); });

Why does the JavaScript need to start with “;”?

Ramu Prasad
Updated on 23-Jun-2020 11:51:06

89 Views

JavaScript uses a semi-colon (;) to avoid any confusion in code, especially the beginning of a new line.ExampleYou can still use the following to avoid any chaos in the code −return {    'var':myVal } // using semi-colon; (function( $ ) {    // }

How and why does 'z'['toUpperCase']() in JavaScript work?

Shubham Vora
Updated on 22-Aug-2022 09:04:13

164 Views

In this tutorial, we will learn how and why ‘z’[‘toUpperCase’]() works in JavaScript. From the given format, we can say that it calls the toUpperCase() method for the ‘z’ string. It will work the same as the toUpperCase() method, which we invoke by taking any string as a reference. Let’s understand the syntax of the ‘z’[‘toUpperCase’]() below. Syntax let result = 'z'['toUpperCase'](); // returns 'Z', string in upper case The above syntax is same as the below. Example let string = 'z'; let result = string.toUpperCase(); Why does ‘z’[‘toUpperCase’]() work? In JavaScript toUpperCase() method is used to convert ... Read More

How to create JavaScript data grid for millions of rows?

Nancy Den
Updated on 23-Jun-2020 11:51:58

207 Views

To display million of rows of data to the users in a grid, use any of the following grids −S. NoGridDescription1DataTablesAllows adding advanced interaction controls to any HTML table.2IngridAllows resizing columns, paging, sorting, row and column styling to tables.3SlickGridUses virtual rendering to allow you to work with hundreds of thousands of items.Let us see the features of SlickGrid −Easily customizableComplete keyboard navigationEasily handles hundreds of thousands of rowsQuick rendering speedColumn autosizing availablePluggable cell formatters availableAllos creating new rows

Is their a JavaScript Equivalent to PHP explode()?

Ankith Reddy
Updated on 23-Jan-2020 07:05:08

2K+ Views

The JavaScript equivalent to PHP explode() is split(). To get the data after the first colon, try to run the following code.Example                    var str = '087000764008:Rank:info:result';          var arr = str.split(":");          document.write(arr[1] + ":" + arr[2]);          

How to hide HTML element with JavaScript?

Shubham Vora
Updated on 10-Sep-2023 07:49:14

43K+ Views

In this tutorial, we will learn how to hide HTML element with JavaScript. Hiding an HTML element can be performed in different ways in JavaScript. In this tutorial, we will see the three most popular ways of doing it − Using the hidden property Using the style.display property Using the style.visibility property Generally, we use the hidden attribute to hide a particular element. We can toggle between hiding and showing the element by setting the hidden attribute value to true or false, respectively. In the other two ways, we use the style object of the element. We ... Read More

Advertisements