Found 9313 Articles for Object Oriented Programming

How to detect the screen resolution with JavaScript?

AmitDiwan
Updated on 11-Sep-2020 06:32:25

1K+ Views

To detect the screen resolution, use the concept of window.screen.For width, use the following −window.screen.availWidthFor height −window.screen.availHeightFollowing is the code to detect the screen resolution −Example Live Demo Document    console.log("Available Width="+window.screen.availWidth);    console.log("Available Height="+window.screen.availHeight); 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 −

What is the 'new' keyword in JavaScript to create an object and how to access values?

AmitDiwan
Updated on 11-Sep-2020 06:29:12

122 Views

The new keyword creates an object in JavaScript. To access values, use the dot notation. Following is the code −Examplefunction newObjectCreation() {    this.firstName = "John"; } var newObject = new newObjectCreation(); newObject.firstName="David"; console.log("The first name="+newObject.firstName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo134.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo134.js The first name=David

Attach event to dynamic elements in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 06:28:00

145 Views

To attach event, use document.addEventListener() in JavaScript. Following is the code −Example Live Demo Document    document.addEventListener('click',function(event){       if(event.target && event.target.id== 'buttonSubmit'){          console.log("Event generation on the button click")       }    }); 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 clicking the submit button, the screenshot is as follows −

Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 06:25:40

309 Views

Let’s say the following is our string −var sentence = "My, , , , , , , Name, , , , is John ,, , Smith";Use regular expression along with split() and join() to remove extra spaces and commas. Following is the code −Examplevar sentence = "My, , , , , , , Name, , , , is John ,, , Smith"; console.log("Before removing extra comma and space="+sentence); sentence = sentence.split(/[\s, ]+/).join(); console.log("After removing extra comma and space="); console.log(sentence)To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo133.js. This will produce ... Read More

How do I display the content of a JavaScript object in a string format?

AmitDiwan
Updated on 11-Sep-2020 06:21:30

209 Views

Use document.getElementById() and display using innerHTML. Following is the code −Example Live Demo Document    var jsonObject="name";    var value = jsonObject.split(" ");    var add = {};    value.forEach(function(v){       add[v] ? add[v]++ : add[v] = "John";    });    document.getElementById("objectId").innerHTML = JSON.stringify(add); 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 RegExp return values between various square brackets? How to get value brackets?

AmitDiwan
Updated on 11-Sep-2020 06:19:01

1K+ Views

Use regular expression with indexes 0,1….N to get the value without brackets. Following is the code −Examplevar regularExpression= /(? node demo132.js The first value without bracket=John Smith The second value without bracket=David Miller

Updating copied object also updates the parent object in JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:30:11

210 Views

No, the parent object won’t get updated. Use Object.assign() with some parameters and check. Following is the code −Examplevar firstObject = { name: 'John' }; var secondObject = { name: 'Carol' }; console.log("Before merging="); console.log(firstObject); var afterMerging = Object.assign({}, firstObject, secondObject); afterMerging.name = 'Smith'; console.log("After merging="); console.log(firstObject);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo131.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo131.js Before merging= { name: 'John' } After merging= { name: 'John' }

JavaScript Check for case insensitive values?

AmitDiwan
Updated on 10-Sep-2020 07:27:55

211 Views

To check for case insensitive values, use regular expression in JavaScript. Following is the code −Examplevar allNames = ['john','John','JOHN']; var makeRegularExpression = new RegExp(allNames.join( "|" ), "i"); var hasValue = makeRegularExpression.test("JOHN"); console.log("Is Present="+hasValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo130.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo130.js Is Present=true

List View Search list on typing with filter like keyword search using jQuery?

AmitDiwan
Updated on 10-Sep-2020 07:25:48

4K+ Views

Let’s the following is our input type, wherein the user will search −Now, use hide() and show() to display only relevant search and hide rest. For example, on typing “Ja”, related keywords like “Java” and “JavaScript” should be visible because it begins with “Ja”.Example Live Demo Document Enter the keyword.. JavaScript John Smith Java David Miller -1) {             $(this).show();             $(this).prev('.subjectName').last().show();          } else {             $(this).hide();     ... Read More

How to hide table rows containing zero value with jQuery?

AmitDiwan
Updated on 10-Sep-2020 07:22:42

1K+ Views

Let’s say the following is our table with product quantity records − qty: qty: qty: qty: Let us now hide using the hide() method. Following is the code −Example Live Demo Document qty: qty: qty: qty:    $('.hideRowContainingZeroValue input').filter(function(){       return +this.value === 0;    }).closest("tr").hide() 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 −

Advertisements