Best JavaScript Code to Create an IMG Element

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

210 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';

Create User Defined Exceptions in C#

Samual Sam
Updated on 23-Jun-2020 12:07:05

350 Views

Like any other programming language, in C#, you can easily create user-defined exception. User-defined exception classes are derived from the Exception class.In the below example, the exception created is not a built-in exception.TempIsZeroExceptionYou can try to run the following code to learn how to create user defined exception in C#.Example Live Demousing System; namespace Demo {    class TestTemperature {       static void Main(string[] args) {          Temperature temp = new Temperature();          try {             temp.showTemp();          } catch(TempIsZeroException e) {       ... Read More

Create Nested While Loop in C#

George John
Updated on 23-Jun-2020 12:06:18

390 Views

For a nested while loop, we have two while loops.The first loop checks for a condition and if the condition is true, it goes to the inner loop i.e. the nested loop.Loop 1while (a

Create New Img Tag with jQuery from JavaScript Object

Anvi Jain
Updated on 23-Jun-2020 12:06:14

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' });

Set Element Visibility with JavaScript

Monica Mona
Updated on 23-Jun-2020 12:03:19

230 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";             }          }          

Set All Outline Properties in a Single Declaration with JavaScript

Paul Richard
Updated on 23-Jun-2020 12:01:37

164 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";          }          

Create Syntax Highlighting Code with JavaScript

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

210 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

JavaScript Code for Multiple Keys Pressed at Once

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

698 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'); });

JavaScript Not In Operator for Checking Object Properties

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

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 { }

Define Global Variable in a JavaScript Function

Sravani S
Updated on 23-Jun-2020 11:56:54

431 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 = …;    }

Advertisements