Found 9313 Articles for Object Oriented Programming

JavaScript Get row count of an HTML table?

AmitDiwan
Updated on 10-Sep-2020 07:02:09

3K+ Views

Let’s say the following is our table − Name Age John21 David22 Bob20 Mike24 To get row count of an HTML table, use .rows.length. Following is the code −Example Live Demo Document Name Age John21 David22 Bob20 Mike24    var allTableData = document.getElementById("tblDemo");    var totalNumbeOfRows = allTableData.rows.length;    console.log("Total Number Of Rows="+totalNumbeOfRows); 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 an H1 element and its text at a particular index in jQuery

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

424 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 −

How to 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

JavaScript Submit textbox on pressing ENTER?

AmitDiwan
Updated on 09-Sep-2020 14:03:49

2K+ Views

The ENTER has the key code 13. We will use the event.keycode to check whether ENTER is pressed or not. Following is the code −Example Live Demo Document    document.getElementById("name").addEventListener("keydown",    function(event) {       if (!event) {          var event = window.event;       }          event.preventDefault();       if (event.keyCode == 13){          login();       }    }, false);    function login(){       console.log("submitted successfully....");    } To run the ... Read More

How to 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

How to use JavaScript map() method to access nested objects?

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

4K+ 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

How to get key name when the value contains empty in an object with JavaScript?

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

716 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

Check whether a value exists in JSON object?

AmitDiwan
Updated on 09-Sep-2020 13:28:22

5K+ Views

Let’s say the following is our object −var apiJSONObject = [    {subjectName:"MySQL"},    {subjectName:"Java"},    {subjectName:"JavaScript"},    {subjectName:"MongoDB"} ]Let’s check for existence of a value “JavaScript” −Examplevar apiJSONObject = [    {subjectName:"MySQL"},    {subjectName:"Java"},    {subjectName:"JavaScript"},    {subjectName:"MongoDB"} ] for(var i=0;i node demo117.js The search found in JSON Object

HTML form action and onsubmit validations with JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:23:41

2K+ Views

Let’s see an example wherein we are validating input text onsubmit −Example Live Demo Document    function validateTheForm(){       var validation = (document.getElementById('txtInput').value == 'gmail');       if(!validation){          alert('Something went wrong...Plese write gmail intext box and click');          return false;       }       return true;    } 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” ... Read More

Advertisements