Javascript Articles

Page 440 of 534

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

Ankith Reddy
Ankith Reddy
Updated on 23-Jun-2020 224 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';

Read More

How to create a new img tag with JQuery, with the src and id from a JavaScript object?

Anvi Jain
Anvi Jain
Updated on 23-Jun-2020 2K+ Views

To create a new img tag in JavaScript, pass an HTML string to the constructor,var myImg = $(''); $(document.createElement(myImg)); myImg.attr('src', responseObject.imgurl);You can also use the following code to create a new img tag with the attributes like src, id, etc −var myImg = $('', {    id: 'id1',    src: exampleimg.png',    alt: 'Alt text' });

Read More

Set whether or not an element should be visible while not facing the screen with JavaScript?

Monica Mona
Monica Mona
Updated on 23-Jun-2020 245 Views

Use the JavaScript backfaceVisibility property to set whether or not an element should be visible while not facing the screen.ExampleYou can try to run the following code to learn how to implement a backfaceVisibility property in JavaScript −Live Demo                    div {             width: 200px;             height: 300px;             background: blue;             color: black;             animation: mymove 2s infinite linear alternate;          }          @keyframes mymove {             to {transform: rotateY(180deg);}          }                     Check below to display backface                Demo             backfaceVisibility Property                function display(x) {             if (x.checked === true) {                document.getElementById("box").style.backfaceVisibility = "visible";             } else {                document.getElementById("box").style.backfaceVisibility = "hidden";             }          }          

Read More

How to set all outline properties in a single declaration with JavaScript?

Paul Richard
Paul Richard
Updated on 23-Jun-2020 176 Views

To set all outline properties in one declaration, use the outline property. It sets the following properties: outline-width, outline-style, and outline-color.ExampleYou can try to run the following code to learn how to work with outline properties −Live Demo                    #box {             border: 2px dashed blue;          }                     This is a div.             Click to set Outline                function display() {             document.getElementById("box").style.outline = "5px solid red";          }          

Read More

Create a syntax highlighting code with JavaScript.

Srinivas Gorla
Srinivas Gorla
Updated on 23-Jun-2020 222 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

Read More

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

Arjun Thakur
Arjun Thakur
Updated on 23-Jun-2020 727 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'); });

Read More

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

Vrundesha Joshi
Vrundesha Joshi
Updated on 23-Jun-2020 2K+ 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 { }

Read More

How to define global variable in a JavaScript function?

Sravani S
Sravani S
Updated on 23-Jun-2020 447 Views

To declare a global variable, use the var at global scope −    var myGlobalVariable;    function display() {       //    } For JavaScript function, assign the property to a window −    function display() {       window. myGlobalVariable = …;    }

Read More

How to create JavaScript data grid for millions of rows?

Nancy Den
Nancy Den
Updated on 23-Jun-2020 355 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

Read More

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

Ramu Prasad
Ramu Prasad
Updated on 23-Jun-2020 190 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( $ ) {    // }

Read More
Showing 4391–4400 of 5,338 articles
« Prev 1 438 439 440 441 442 534 Next »
Advertisements