Skip to content

Commit 9bfc4bc

Browse files
committed
updated the whole code and made it easy
1 parent 35cc122 commit 9bfc4bc

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

Methods.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
window.onload = function () {
2-
const listOfMethods = document.getElementsByClassName("CThis")
3-
let methodName = []
4-
let content = []
5-
for (let i = 0; i < listOfMethods.length; i++) {
6-
const cont = listOfMethods[i];
7-
methodName.push(cont.getAttribute('as'))
8-
content.push(cont.innerHTML)
2+
let definition = document.getElementsByTagName('def')
3+
let methodNames = []
4+
let methodContent = []
5+
let methodDefTags = []
6+
7+
for (let i = 0; i < definition.length; i++) {
8+
let methodDefs = definition[i];
9+
methodDefs.style.visibility = 'hidden'
10+
methodNames.push(methodDefs.getAttribute('name'))
11+
methodContent.push(methodDefs.innerHTML)
12+
methodDefTags.push(methodDefs)
913
}
1014

11-
var toPaste = document.getElementsByClassName("PHere")
12-
for (let j = 0; j < methodName.length; j++) {
13-
let methodN = methodName[j];
14-
for (let i = 0; i < toPaste.length; i++) {
15-
var element = toPaste[i];
16-
const method = element.getAttribute('as')
17-
if (methodN == method) {
18-
element.innerHTML = content[j]
19-
} else {
20-
continue
21-
}
15+
for (let j = 0; j < methodNames.length; j++) {
16+
const methodName = methodNames[j];
17+
let methodCall = document.getElementsByTagName(methodName)
18+
for (let k = 0; k < methodCall.length; k++) {
19+
let tag = methodCall[k];
20+
tag.innerHTML = methodContent[j]
2221
}
2322
}
23+
24+
for (var l = 0; l < methodDefTags.length; l++) {
25+
console.log(methodDefTags[l])
26+
methodDefTags[l].remove()
27+
}
2428
}

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,33 @@
44
A package that can handle methods in html(for non repetative code).
55

66
# What is Methods?
7-
Have you ever get frustated of writing the same code in html to get what you want. Then here you are use this package? to make it easy with just a class and an attribute called **"as"**.
7+
Have you ever get frustated of writing the same code in html to get what you want. Then here you are use this package? to make it easy by defining your method using a **"def"** tag and **"name"** attribute to name your method.
88

99
# Usage:
1010
- Methods package is very simple to use.
11-
- All you need to do is to set the class name to ***CThis*** which means **copy this** and the attribute I defined basically called as ***as*** which should be the name of your method or the code you want to copy and use.
12-
- After that set the class name to ***PHere*** which means **paste here** where ever you want to use that code in your HTML.
11+
- All you need to do is to define the method using **"def"** tag and **"name"** attribute.
12+
- And then to call the method you can use the value you provided in name attribute.
1313
- That's it.
1414
**Note: You can copy any code that means if you want to copy the code which is at the bottom it will work.**
1515

1616
# Example:
1717

1818
```html
19-
<div class="CThis" as="test1">Writing this code at the beginning</div>
20-
<div class="PHere" as="test1"></div>
21-
<div class="PHere" as="BottomCode"></div>
22-
<div class="CThis" as="BottomCode">This is the code at bottom yet it will work</div>
19+
<test></test>
20+
<commit></commit>
21+
<!--use the method name as the tag name-->
22+
<def name="test">
23+
<p>Help</p>
24+
</def>
25+
<def name="commit">
26+
<p>commit</p>
27+
</def>
2328
```
2429

30+
## NOTE:
31+
- Remember that these def tags will be removed once the page is loaded so that you cannot see the code inside this def tags
32+
- So the code inside these tags only works once you call them using as a tag name
33+
- Run [index.html](https://github.com/Chandra-sekhar-pilla/HTML-methods/blob/main/index.html) file and inspect the changes in html so you can know more.
34+
2535
# Output:
26-
![MethodsOutput](https://github.com/Chandra-sekhar-pilla/HTML-methods/blob/main/Resources/Output.png)
36+
![MethodsOutput](https://github.com/Chandra-sekhar-pilla/HTML-methods/blob/main/Resources/outPutv2.png)

Resources/outPutv2.png

3.56 KB
Loading

index.html

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@
88
<script src="Methods.js"></script>
99
</head>
1010
<body>
11-
<div class="CThis" as="test1">Writing this code at the beginning</div>
12-
<p>The above code is the actual code and the below code is the copied one.</p>
13-
<div class="PHere" as="test1"></div>
14-
<div class="PHere" as="BottomCode"></div>
15-
<p> leaving a space inorder to make sure the bottom one is the actual code and the above one is copied one.</p>
16-
<div class="CThis" as="BottomCode">This is the code at bottom yet it will work</div>
11+
12+
<!--use the method name as the tag name i.e here test and commit are the method names-->
13+
<test></test>
14+
<commit></commit>
15+
16+
<!--
17+
define the methods. That means here def tag is used to define the methods and name attribute as the method name
18+
Remember that these def tags will be removed once the page is loaded so that you cannot see the code inside this def tags
19+
So the code inside these tags only works once you call them using as a tag name
20+
run this file and inspect the changes in html so you can know more
21+
-->
22+
<def name="test">
23+
<p>Methods.js</p>
24+
</def>
25+
<def name="commit">
26+
<p>commit</p>
27+
</def>
1728
</body>
1829
</html>

0 commit comments

Comments
 (0)