Random Color Generator in JavaScript

AmitDiwan
Updated on 16-Jul-2020 06:50:51

1K+ Views

Following is the syntax to generate random color in JavaScript −$("#yourIdName").css("background-color", yourCustomFunctionNameToGetRandomColor());Following is the JavaScript code −Example Live Demo Document Click the button to get random color    //The getColorCode() will give the code every time.    function getColorCode() {       var makeColorCode = '0123456789ABCDEF';       var code = '#';       for (var count = 0; count < 6; count++) {          code =code+ makeColorCode[Math.floor(Math.random() * 16)];       }       return code;    }    //Function ... Read More

Hide Div That Contains Specific Text with JavaScript

AmitDiwan
Updated on 16-Jul-2020 06:46:35

1K+ Views

At first, you need to extract the extract the div class with the help of getElementsByClassName() and iterate over the for loop and use the OR condition to show the specific text.Also, set yourDiv.style.display=’none’.Following is the JavaScript code −Example Live Demo Document header content footer    let attribute = document.getElementsByClassName('block');    for (let i = 0; i < attribute.length; i++) {       let impDiv = attribute[i];       let value = impDiv.innerHTML.trim();       if (value == 'header' || value == ... Read More

Preview Image Before Uploading in JavaScript

AmitDiwan
Updated on 16-Jul-2020 06:43:59

234 Views

To upload an image, use FileReader() in JavaScript. Following is the JavaScript code −Example Live Demo Document    function readImage(fileInput) {       if (fileInput.files && fileInput.files[0]) {          var takingInputFile = new FileReader();          takingInputFile.onload = function(event) {             $('#chooseImage').attr('src', event.target.result);          }          takingInputFile.readAsDataURL(fileInput.files[0]);       }    }    $("#yourImage").change(function() {       readImage(this);    }); To run the above program, save the file name anyName.html(index.html) and right click on the file and select the option Open with live server in VS code editor.Output

Explain Error Name Values in JavaScript with Examples

AmitDiwan
Updated on 15-Jul-2020 14:14:45

339 Views

The error name value is used for setting or returning the error name. The error name can return the below values.Sl.NoError Name & Description1EvalErrorIt represents an error in the eval() function2RangeErrorIt happens when a numeric value is out of its range3ReferenceErrorIt happens when an illegal reference has occured4SyntaxErrorIt represents a syntax error5TypeErrorIt represents a type error6URIErrorIt represents an error in the encodeURI().ExampleFollowing is the code for the error name values in JavaScript − Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {     ... Read More

Explain Try and Catch Statements in JavaScript with Examples

AmitDiwan
Updated on 15-Jul-2020 14:11:20

232 Views

The try statement allows us execute a block of code and test for errors. Those errors are then caught and handle by the catch statement.Following is the code for try and catch statement in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Try and catch in JavaScript CLICK HERE Click on the above button to call a variable before it is defined ... Read More

Test and Execute Regular Expression in JavaScript

AmitDiwan
Updated on 15-Jul-2020 14:08:31

275 Views

Following is the code for testing and executing a regular expression in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 20px;       font-weight: 500;    } Testing and executing regular expressions CLICK HERE Click on the above button to test and execute the regular expression    let sampleEle=document.querySelector('.sample');    let resEle = document.querySelector('.result');    let str = 'Hello world. This is a beautiful world';    sampleEle.innerHTML ... Read More

JavaScript Regular Expression Modifiers Explained with Examples

AmitDiwan
Updated on 15-Jul-2020 14:05:54

623 Views

The JavaScript regular expression modifiers are optional part of a regular expression and allow us to perform case insensitive and global searchers. The modifiers can also be combined together.Following are the modifiers −ModifierDescriptiongIt enables global matching and returns all the matched results instead of stopping at first matchiIt enables case insensitive matchingmIt enables multiline matchingExampleFollowing is the code for strict comparison in JavaScript switch statement − Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: ... Read More

Strict Comparison in JavaScript Switch Statement

AmitDiwan
Updated on 15-Jul-2020 14:01:58

408 Views

The JavaScript switch statement only uses strict comparison (===) and doesn’t converts type if matches are not found using strict comparison and will immediately execute the default statement.Following is the code for strict comparison in JavaScript switch statement −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } JavaScript Switch statement strict comparison Enter day 1-7 CHECK Click on the above button to check if switch ... Read More

Common Code Blocks in JavaScript Switch Statement

AmitDiwan
Updated on 15-Jul-2020 13:56:19

262 Views

Following is the code to implement common code blocks in JavaScript switch statement −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } JavaScript Switch statement Enter day 1-7 CHECK Click on the above button to check which day it is    let dayVal = document.querySelector(".day");    let resEle = document.querySelector(".result");    document.querySelector(".Btn").addEventListener("click", () => {       switch (parseInt(dayVal.value)) {     ... Read More

Verify If a JavaScript Object is an Array with Examples

AmitDiwan
Updated on 15-Jul-2020 13:53:49

150 Views

The JavaScript Array.isArray() method is used to verify if a JavaScript object is an array or not based on the Boolean value returned by it.Following is the code to verify if JavaScript object is an array −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 20px;       font-weight: 500;    } Verify if a JavaScript object is array CLICK HERE Click on the above button to verify if the above ... Read More

Advertisements