Found 9150 Articles for Object Oriented Programming

How do you make a button that adds text in HTML 'input'?

AmitDiwan
Updated on 13-Mar-2025 12:49:45

3K+ Views

Let’s say the following is our HTML button −Click the button to add the input into the belowText BoxUse document.getElementById() to add a text in on button click. Following is the code −Example Live Demo Document Click the button to add the input into the below Text Box  document.getElementById("clickButton").addEventListener("click", () =>{ document.getElementById("readTheInput").value += "JavaScript";  }); 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 ... Read More

Converting any string into camel case with JavaScript removing whitespace as well

AmitDiwan
Updated on 07-Sep-2020 08:52:41

1K+ Views

In order to convert string into camel case, you need to lowercase the first letter of the word and the first letter of the remaining words must be in capital.Following is the code to convert any string into camel case −Examplefunction convertStringToCamelCase(sentence) {    return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g,    function(camelCaseMatch, i) {       if (+camelCaseMatch === 0)          return "";       return i === 0 ? camelCaseMatch.toLowerCase() :       camelCaseMatch.toUpperCase();    }); } console.log(convertStringToCamelCase("Add two variables"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is ... Read More

How to display only the current year in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:51:37

448 Views

To display only the current year, use getFullYear(). Following is the code −Example Live Demo Document The Current Year=    document.getElementById("theCurrentYear").innerHTML = new    Date().getFullYear(); 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 −

Strip quotes with JavaScript to convert into JSON object?

AmitDiwan
Updated on 07-Sep-2020 08:50:06

2K+ Views

For this, you can use replace() along with parse(). Following is the code −Examplevar studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("The actual object="); console.log(studentDetails); var removeFirstAndLast = studentDetails.substring(1, studentDetails.length-1) var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`) console.log(removeDoubleQuotes) var output = JSON.parse(removeDoubleQuotes); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo103.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo103.js The actual object= "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" {"name": "John", "subjectName": "Introduction To JavaScript"} { name: 'John', subjectName: 'Introduction To JavaScript' }Read More

Copy objects with Object.assign() in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:48:54

167 Views

The Object.assign() method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target.The syntax is as follows −Object.assign(target, ...source objects);Following is the code to copy object −Examplevar object = {first: second => second + 1000} var object2= Object.assign({}, object); console.log("The result="); console.log(object2.first(1000));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo102.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo102.js The result= 2000Read More

Is there a DOM function which deletes all elements between two elements in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:47:19

307 Views

Let’s say the following are our elements −My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol ENDWe need to remove theelement and its content. Theelements are in between the START and END.To remove elements between two elements, use the concept of remove(). Following is the code −Example Live Demo Document START My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol END const startingPoint = document.querySelector("nav"); const endingPoint = document.querySelector("footer"); ... Read More

Replace HTML div into text element with JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:45:24

823 Views

For this, use document.querySelectorAll(). With that, also use the getElementsByClassName(). Following is the code −Example Live Demo Document My Name is John My h6 value must be here... My h6 value must be here... My h6 value must be here...    const allSpan = document.querySelectorAll('.my-main-div-class span'),    repaceAboveText =    document.getElementsByClassName('uniqueId')[0].textContent;    for (var newElement of allSpan){       newElement.textContent=repaceAboveText    } 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 −

JavaScript Regex to remove leading zeroes from numbers?

Alshifa Hasnain
Updated on 06-Jan-2025 22:49:55

1K+ Views

In this article, we will learn to remove leading zeroes from numbers in JavaScript using Regex. When working with numeric strings in JavaScript, it’s common to encounter unwanted leading zeroes. These zeroes can cause issues when parsing numbers, formatting data, or displaying user-friendly outputs. Why Remove Leading Zeroes in JavaScript? Leading zeroes can lead to inconsistencies in numeric string representation. For instance − When parsing numbers, leading zeroes might result in incorrect interpretations. Displaying numbers with leading zeroes can confuse users or look unprofessional. Clean strings are ... Read More

Update array of objects with JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:42:02

430 Views

Let’s say the following are our array of objects −var studentDetails = [    { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']},    {firstName: "David", listOfSubject: ['Java', 'C'] }]We need to add the following in the already created array of objects −{firstName: "Bob", listOfSubject: ['JavaScript']};Examplevar studentDetails = [    { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']},    {firstName: "David", listOfSubject: ['Java', 'C']}];    updateThisObject = {firstName: "Bob", listOfSubject: ['JavaScript']};    function forLoopExample(studentObjects, updateObj){    for(var index = 0;index < studentObjects.length; index++) {       if(updateObj.listOfSubject.join("") ===          studentObjects[index].listOfSubject.join("")) {          studentObjects[index] = updateObj;       ... Read More

How to add properties from one object into another without overwriting in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:40:22

643 Views

Let’s say the following are our objects −var first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000}You can use the concept of hasOwnProperty() to add properties from one object to another. Following is the code −Examplevar first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000} function addPropertiesWithoutOverwritting(first, second) {    for (var key2 in second) {       if (second.hasOwnProperty(key2) && !first.hasOwnProperty(key2)) {          first[key2] = second[key2];       }    }    return first; } console.log(addPropertiesWithoutOverwritting(first, second))To run ... Read More

Advertisements