Set Text in Strong Tag with JavaScript

AmitDiwan
Updated on 10-Sep-2020 07:04:29

1K+ Views

At first, set the element −Replace This strong tagThe id attribute set above would be used set the text using # −$(document).ready(function(){    $("#strongDemo").html("Actual value of 5+10 is 15....."); });Example Live Demo Document Replace This strong tag    $(document).ready(function(){       $("#strongDemo").html("Actual value of 5+10 is 15.....");    }); 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 −

Place H1 Element and Text at Specific Index in jQuery

AmitDiwan
Updated on 10-Sep-2020 06:59:49

533 Views

To set an element and it’s text, use the text() method in jQuery. With that, use the :nth-child selector to place it at a particular position. Following is the code −Example Live Demo Document JavaScript MySQL MongoDB Java C#    $('h1:nth-child(4)').text("Python"); 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 −

Group JSON Data in JavaScript

AmitDiwan
Updated on 09-Sep-2020 14:11:54

3K+ Views

To group JSON data, you need to extract all the keys and use the push(). Following is the code −Examplevar details= {    "1":    {       name:"John"    },    "2":    {       name:"John"    },    "3":    {       name:"David"    }    var objectWithGroupByName = {};    for (var key in details){       var name = details[key].name;    if (!objectWithGroupByName[name]){       objectWithGroupByName[name] = [];    }    objectWithGroupByName[name].push(details[key]); } console.log(objectWithGroupByName);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo122.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo122.js {    John: [ { name: 'John' }, { name: 'John' } ],    David: [ { name: 'David' } ] }

Call a JavaScript Class Object with Variable

AmitDiwan
Updated on 09-Sep-2020 14:09:58

1K+ Views

Let’s say the following is our variable −var message = 'This is the Class Demo';Following is our object, var object = new FirstClass(message)The class FirstClass −class FirstClass{    constructor( message){       this.message = message;    } }We will use eval() to call a JavaScript class object with variable. Following is the code −Exampleclass FirstClass{    constructor( message){       this.message = message;    } } var message = 'This is the Class Demo'; var object = new FirstClass(message) console.log(eval(object).message);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo121.js.OutputThis ... Read More

Get a Subset of JavaScript Object's Properties

AmitDiwan
Updated on 09-Sep-2020 13:55:30

170 Views

Let’s say the following is our object −var details ={    firstName: "John",    lastName: "Smith",    age:21,    subjectName:"JavaScript",    marks:89,    coutryName:"US",    collegeName:"MIT",    passoutYear:2018 };Following is the code to fetch only a subset −Examplevar details ={    firstName: "John",    lastName: "Smith",    age:21,    subjectName:"JavaScript",    marks:89,    coutryName:"US",    collegeName:"MIT",    passoutYear:2018 }; var selectedFieldFromObjectDetails = (({ firstName, subjectName, marks, passoutYear }) => ({ firstName, subjectName, marks, passoutYear }))(details); console.log(selectedFieldFromObjectDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo120.js.OutputThis will produce the following output −PS ... Read More

Convert Comma Separated Text in DIV into Separate Lines with JavaScript

AmitDiwan
Updated on 09-Sep-2020 13:52:21

3K+ Views

Let’s say the following is our comma separated text in div − This, is, the, first, JavaScript, program To convert comma separated text into separate lines, you need to use trim() along with split() on the basis of comma(, ).Example Live Demo Document This, is, the, first, JavaScript, program    var allTheData = document.querySelector('.commaSeparated').textContent.trim().split(', ')    var separateList = ''    allTheData.forEach(function(value) {       separateList += '' + value + '';    });    separateList += '';    document.querySelector(".commaSeparated").innerHTML = separateList; To run the ... Read More

Use JavaScript Map Method to Access Nested Objects

AmitDiwan
Updated on 09-Sep-2020 13:36:30

5K+ Views

Let’s say the following is our nested objects −var details = [    {       id:"101",       firstName:"John",       lastName:"Smith",       age:25,       countryName:"US",       subjectDetails: {          subjectId:"Java-101",          subjectName:"Introduction to Java"       },    },    {       "uniqueId": "details_10001"    } ]Use map() along with typeOf to access nested objects. Following is the code −Examplevar details = [    {       id:"101",       firstName:"John",       lastName:"Smith",       ... Read More

Get Key Name When Value is Empty in JavaScript Object

AmitDiwan
Updated on 09-Sep-2020 13:32:44

838 Views

Let’s say the following is our object −var details = {    firstName: 'John',    lastName: '',    countryName: 'US' }Use Object.keys() along with find() to get the key name with empty value. Following is the code −Examplevar details = {    firstName: 'John',    lastName: '',    countryName: 'US' } var result = Object.keys(details).find(key=> (details[key] === '' || details[key] === null)); console.log("The key is="+result);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo118.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo118.js The key is=lastNameRead More

Maximum Sum Subsequence with At Least K Distant Elements in C++

Ayush Gupta
Updated on 09-Sep-2020 13:30:59

167 Views

In this tutorial, we will be discussing a program to find maximum sum subsequence with at-least k distant elements.For this we will be provided with an array containing integers and a value K. Our task is to find the subsequence having maximum sum such that all the elements are at least K elements apart.Example Live Demo#include using namespace std; //finding maximum sum subsequence int maxSum(int arr[], int N, int k) {    int MS[N];    MS[N - 1] = arr[N - 1];    for (int i = N - 2; i >= 0; i--) {       if (i ... Read More

Maximum Sum Subarray with Same Start and End Values in C++

Ayush Gupta
Updated on 09-Sep-2020 13:29:00

194 Views

In this tutorial, we will be discussing a program to find maximum sum subarray such that start and end values are same.For this we will be provided with an array containing integers. Our task is to find the subarray with the maximum sum such that the elements are both its ends are equal.Example Live Demo#include using namespace std; //finding the maximum sum int maxValue(int a[], int n) {    unordered_map first, last;    int pr[n];    pr[0] = a[0];    for (int i = 1; i < n; i++) {       pr[i] = pr[i - 1] + a[i]; ... Read More

Advertisements