Is an Empty iframe src Valid in JavaScript

AmitDiwan
Updated on 02-Sep-2020 06:37:59

1K+ Views

For empty iframe src, use the element and set it like the following for an empty src −Example Live Demo Document To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Transform Object of Objects to Object of Array of Objects in JavaScript

AmitDiwan
Updated on 02-Sep-2020 06:19:44

776 Views

To transform object of objects to object of array of objects, use the concept of Object.fromEntries() along with map().Exampleconst studentDetails = {    'details1': {Name: "John", CountryName: "US"},    'details2': {Name: "David", CountryName: "AUS"},    'details3': {Name: "Bob", CountryName: "UK"}, }; console.log(    Object.fromEntries(Object.entries(studentDetails).map(([key,    value]) => [key, [value]])) );To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo45.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo45.js {    details1: [ { Name: 'John', CountryName: 'US' } ],    details2: [ { Name: 'David', CountryName: 'AUS' } ],    details3: ... Read More

Why HTML File Can't Find JavaScript Function from Sourced Module

AmitDiwan
Updated on 02-Sep-2020 06:14:29

2K+ Views

This may happen if you haven’t used “export” statement. Use “export” before the function which will be imported into the script file. The JavaScript file is as follows which has the file name demo.js.demo.jsconsole.log("function will import"); export function test(){    console.log("Imported!!!"); }Here is the “index.html” file that imports the above function −index.htmlExample Live Demo Document    import { test } from "./demo.js"    test(); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS ... Read More

Refresh Page After Clearing All Form Fields in jQuery

AmitDiwan
Updated on 01-Sep-2020 12:30:34

2K+ Views

To refresh page, use the location.reload() in JavaScript. The sample JavaScript code is as follows −Example Live Demo Document UserName: Password: Refresh Page    $('#RefreshPage').click(function() {       location.reload();    }); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −After filling the form, the snapshot is as follows −When you click the button “Refresh Page”, the page will refresh and the following output is visible −

Get Focus on Textbox After Pressing Mouse Cursor in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:27:37

436 Views

Let’s say the following is our input text −StudentName:To lose input hints on pressing mouse cursor, use onFocus and onBlur concept.Example Live Demo Document StudentName:    function guessFocus() {       if (this.value == this.defaultValue)          this.value = '';    }    function guessBlur(event) {       if (this.value == '')       this.value = this.defaultValue;    }    var event = document.getElementById('studentName');    event.onfocus = guessFocus;    event.onblur = guessBlur; To run the above program, save the file name “anyName.html(index.html)” and ... Read More

Inherit CSS Properties of Parent Element Using JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:23:55

447 Views

You can use classList.add() along with append(). Use the document.createElement() to create a new div and the classList.add() would add the CSS class.(.class selector).Example Live Demo Document    .add-all-subject {       display: grid;       height: 100px;       width: 100px;       grid-template-columns: 2fr 2fr 2fr 2fr 2fr;       grid-template-rows: 2fr;       grid-column-gap: 5.9rem;       grid-row-gap: 2rem;       color: red;       border: 2px solid red;    } Add Subjects    const addSubjectArea = document.getElementById("add-subject"); ... Read More

Capitalize the First Letter of Each Word in a String Using JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:18:46

3K+ Views

At first, you need to split() the string on the basis of space and extract the first character using charAt(). Use toUpperCase() for the extracted character.Examplefunction capitalizeTheFirstLetterOfEachWord(words) {    var separateWord = words.toLowerCase().split(' ');    for (var i = 0; i < separateWord.length; i++) {       separateWord[i] = separateWord[i].charAt(0).toUpperCase() +       separateWord[i].substring(1);    }    return separateWord.join(' '); } console.log(capitalizeTheFirstLetterOfEachWord("my name is john"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo43.js.OutputThis will produce the following output with first letter capitalize −PS C:\Users\Amit\JavaScript-code> node demo43.js My Name ... Read More

Remove Same Values from Array in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:14:07

203 Views

Let’s say the following is our array with similar values −const listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John'];To remove similar values from array, use the concept of set(). Following is the code −Exampleconst listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John']; console.log("The value="+listOfStudentName); const doesNotContainSameElementTwice = [...new Set(listOfStudentName)]; console.log("The Array="); console.log(doesNotContainSameElementTwice)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo42.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo42.js The value=John, Mike, John, Bob, Mike, Sam, Bob, John The Array= [ 'John', 'Mike', 'Bob', 'Sam' ]Read More

Check If Value is Empty in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:13:00

21K+ Views

Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.Example Live Demo Document USERNAME:    function checkingUserName() {       var username = document.forms["register"]["username"].value;       if (username == null || username == "") {          alert("Please enter> the username. Can’t be blank or empty !!!");          return false;       }    } To run the above program, save ... Read More

Display Substring from Object Entries in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:10:22

289 Views

Yes, you can use Object.fromEntries() along with substr(). Under substr(), mention the index from where to begin the substring and the length.Exampleconst originalString = {    "John 21 2010" :1010,    "John 24 2012" :1011,    "John 22 2014" :1012,    "John 22 2016" :1013, } const result = Object.fromEntries(Object.entries(originalString). map(([k, objectValue])=> [k.substr(0, k.length-5), objectValue])); console.log(result)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo41.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo41.js { 'John 21': 1010, 'John 24': 1011, 'John 22': 1013 }

Advertisements