https://github.com/xenioushk/frequently_used_js_codes
https://github.com/xenioushk/frequently_used_js_codes
javascript javascript-examples-beginners javascript-snippets
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/xenioushk/frequently_used_js_codes
- Owner: xenioushk
- Created: 2022-07-22T08:02:51.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-15T08:07:22.000Z (over 1 year ago)
- Last Synced: 2025-01-12T13:26:49.813Z (over 1 year ago)
- Topics: javascript, javascript-examples-beginners, javascript-snippets
- Homepage: https://bluewindlab.net
- Size: 15.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Frequently Used JS Codes
## Get all the child elements
```javascript
var all_checkbox = document.getElementById("gg_data_tags").querySelectorAll('input[type="checkbox"]')
```
## Loop for the child elements
```javascript
all_checkbox.forEach(function (item) {
item.checked = false
})
```
## Add Class to an element
```javascript
this.result_player_two.classList.add("current-player")
```
## Add Multiple Classes to an element
````javascript
this.result_player_two.classList.add("current-player", "class_2", "class_3")```
## Remove Class from an element
```javascript
this.result_player_one.classList.remove("current-player")
````
## Add 'id' attribute to an element
```javascript
this.result_player_one.setAttribute("id", "my-id")
```
## Add custom attribute to an element
```javascript
this.result_player_one.setAttribute("data-score", "10")
```
## Remove 'class' attribute from an element
```javascript
this.result_player_one.removeAttribute("class")
```
## Remove Multiple Classes to an element
```javascript
this.result_player_two.classList.remove("class_2", "class_3")
```
## Force checkbox to check
````javascript
document.getElementById("checkbox").checked = true;```
## Force checkbox to uncheck
```javascript
document.getElementById("checkbox").checked = false;
````
## Push data to array
```javascript
var sel_authors = new Array()
sel_authors.push("john", "mahbub", "alam", "khan")
```
## Random value betwen two numbers
```javascript
randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
```
## Join Array
```javascript
const fruits = ["Banana", "Orange", "Apple", "Mango"]
let text = fruits.join()
```
## Array Map
```javascript
const numbers = [65, 44, 12, 4]
const newArr = numbers.map(myFunction)
function myFunction(num) {
return num * 10
}
```
### More example
You can check more javascript example codes from [here](https://github.com/xenioushk/javascript_101).
## Acknowledgement
- [bluewindlab.net](https://bluewindlab.net)