I have a very basic code. It simply changes the style of the button on click. It works fine when used getElementById but I wanna do it the XPATH way. I'm new to JavaScript. What I'm doing wrong and how can I achieve this?
The code:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to change the layout of this paragraph</p>
<button onclick="myFunction()">Click Me!</button>
<script>
function myFunction() {
let x = document.getElementByXpath("//html[1]/body[1]/button[1]");
x.style.fontSize = "25px";
x.style.color = "red";
}
</script>
</body>
</html>
Look at document.evaluate()
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
function myFunction() {
let x = getElementByXpath("//html[1]/body[1]/button[1]");
x.style.fontSize = "25px";
x.style.color = "red";
}
<p id="demo">Click the button to change the layout of this paragraph</p>
<button onclick="myFunction()">Click Me!</button>
Related
I have a very basic code. It simply changes the style of the button on click. It works fine when used getElementById but I wanna do it the XPATH way. I'm new to JavaScript. What I'm doing wrong and how can I achieve this?
The code:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to change the layout of this paragraph</p>
<button onclick="myFunction()">Click Me!</button>
<script>
function myFunction() {
let x = document.getElementByXpath("//html[1]/body[1]/button[1]");
x.style.fontSize = "25px";
x.style.color = "red";
}
</script>
</body>
</html>
Look at document.evaluate()
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
function myFunction() {
let x = getElementByXpath("//html[1]/body[1]/button[1]");
x.style.fontSize = "25px";
x.style.color = "red";
}
<p id="demo">Click the button to change the layout of this paragraph</p>
<button onclick="myFunction()">Click Me!</button>
Here is my code snippet:
function myFunction() {
var x = document.getElementById("myTextarea").value;
document.getElementById("demo").innerHTML = x;
}
<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea"></textarea>
<button type="button" onclick="myFunction()">Transform into link</button>
<script src="links.js"></script>
</body>
</html>
Somehow, the created link is not opened when I click on it. Kind of stuck at this point.
You were doing it right, you just needed to use href instead of innerHTML.
function myFunction() {
var x = document.getElementById("myTextarea").value;
document.getElementById("demo").href = x;
}
<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea"></textarea>
<button type="button" onclick="myFunction()">Transform into link</button>
My Link!
<script src="links.js"></script>
</body>
</html>
Why does the alert window not show the button's width here? I looked through similar questions, but it looks like my question is too simple to have an answer there.
<!DOCTYPE html>
<html>
<body>
<button id="jock" onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
alert(document.getElementById("jock").style.width);
}
</script>
</body>
</html>
Use offsetWidth instead of style width. This will get the elements true width value.
<!DOCTYPE html>
<html>
<body>
<button id="jock" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert(document.getElementById("jock").offsetWidth);
}
</script>
</body>
</html>
There is no style attribute declared in the element. So document.getElementById("jock").style.width may not work,
But getComputedStyle can be used the get the width of the button in the window.
function myFunction() {
var elem1 = document.getElementById("jock")
var style = window.getComputedStyle(elem1, null);
console.log(style.width)
}
<button id="jock" onclick="myFunction()"> Try it
</button>
I am running the code from w3schools to add button dynamically to the page. Here is the code.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to make a BUTTON element with text.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var btn = document.createElement("BUTTON");
var t = document.createTextNode("CLICK ME");
btn.appendChild(t);
document.body.appendChild(btn);
}
</script>
</body>
</html>
But this dynamic addition doesn't work when I have a <form> tag. What changes in the JavaScript I can make to accommodate dynamic button without having to remove the <form> tag?
function myFunction() {
var btn = document.createElement("BUTTON");
var t = document.createTextNode("CLICK ME");
btn.appendChild(t);
document.getElementById('theForm').appendChild(btn);
}
</script>
<p>Click the button to make a BUTTON element with text.</p>
<button onclick="myFunction()">Try it</button>
<form id="theForm">
</form>
how to place a TextNode into a div instead of body, thank you in advance!
Sorry if I am so unexperienced.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to create a h1 element with some text.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var h = document.createElement("H1");
var t = document.createTextNode("Hello");
h.appendChild(t);
document.body.appendChild(h);
}
</script>
</body>
</html>
One way of doing it would be to add a div to Body and then look for it using getElementById
<body>
<div id="myButtonContainer">
<p>Click the button to create a h1 element with some text.</p>
<button onclick="myFunction()">Try it</button>
</div>
<div id="myDiv"></div>
<script>
function myFunction() {
var myButtonContainer = document.getElementById("myButtonContainer");
myButtonContainer.style.display='none';
var h = document.createElement("H1");
var t = document.createTextNode("Hello");
h.appendChild(t);
var myDiv = document.getElementById("myDiv");
myDiv.appendChild(h);
}
</script>
</body>
You can check the example here: http://jsfiddle.net/jmgwya58/1/