Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have text that contain HTML elements. For example like this
<div>
<div>content</div>
<div>
<div id="myId">
<p>
<span>content</span>
<span>content</span>
</p>
</div>
</id>
</div>
I want to get innerHTML of some element from this text using javascript like this example
// HTML variable contain above HTML text
var innerHTML = HTML.getElementById('myId').innerHTML;
Is it possible? Or other way like this way?
You could use a document fragment.
var HTML = "<div>\
<div>content</div>\
<div>\
<div id=\"myId\">\
<p>\
<span>content</span>\
<span>content</span>\
</p>\
</div>\
</div>\
</div>";
// create a fake document
var df = document.createDocumentFragment();
// create a placeholder node to hold the innerHTML
var el = document.createElement('body');
el.innerHTML = HTML;
// append to the document fragment
df.appendChild(el);
// execute .getElementById
console.log(df.getElementById('myId').innerHTML);
Create a new element based on your string as described in this question: Creating a new DOM element from an HTML string using built-in DOM methods or prototype.
After that you can operate on your new sub-tree with whatever DOM methods you want. You don't need to actually attach it to the page unless you want to use methods that would depend on actual rendering.
var HTML =
'<div>\
<div>content</div>\
<div>\
<div id="myId">\
<p>\
<span>content</span>\
<span>content</span>\
</p>\
</div>\
</id>\
</div>';
var elements = document.createElement('div');
elements.innerHTML = HTML;
var targetElement = elements.querySelectorAll("#myId");
console.log(targetElement)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a html like the below,
<div id ="b1">
<div class = "test1" value = 100> </div>
</div>
I need the Value 100 to be displayed in the HTML inside the particular div.
I used the below JS code,
Var target = document.getElementById('b1');
var test = target.getElementsByClassName('test');
$(test).innerHTML = $(this).attr('value');
It didnt work.
Could someone please help?
Many thanks.
The value attribute isn't available on a <div>
Use .innerHTML to change the value of the div
HTML/Javascript change div content
Also, you can use getElementsByClassName on the document itself, no need to get the parent <div> first
Since you're only expecting 1 result, well need to select the first index of the nodelist returned by getElementsByClassName
Javascript: How to get only one element by class name?
var test = document.getElementsByClassName('test1')[0];
test.innerHTML = 100;
<div id="b1">
<div class="test1"></div>
</div>
var test= document.getElementById('b2');
test.innerHTML=test.getAttribute("value")
<div id ="b1">
<div class = "test1" id ="b2" value="100" > </div>
</div>
You have entered the wrong className, and you cannot use this because it refers to the parent object in this case (window object)
you also cannot add a data attribute to a div, you can use a special dataset attribute data-value:
<div id="b1">
<div class="test1" data-value="100"> </div>
</div>
var target = document.getElementById('b1');
var test = target.getElementsByClassName('test1')[0];
test.innerHTML = test.dataset.value;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm working on a grade holder project and I have an html class that holds one of the grades.
<span class="tooltip">
<span class="grade">83.49%</span>
</span>
I wanted to know how I could use JavaScript to turn the 83.49 into a 90.
I tried using the getElementsByClass but I wasn't actually sure of how to go about setting it up. All I need to do is change the specific grade.
if you have used grade class only at this place you can use this solution :-
document.getElementsByClassName("grade")[0].innerText = "90";
Use querySelector():
document.querySelector(".tooltip .grade").innerText = "90%";
<span class="tooltip">
<span class="grade">83.49%</span>
</span>
document.getElementsByClassName('grade')[0].innerText = '90%';
Note: getElementsByClassName returns array-like collection of elements.
Try with Math.ceil()(is round the number to higher value) .Use this (num/10)*10 formula to convert the number round of 10
First replace the % value form text.
Then parse the string to number using parseFloat()
Then apply above formula
var elem = document.querySelectorAll('.grade');
Array.from(elem,(a,b)=>{
var str = a.textContent.replace('%', '').trim();
a.textContent = Math.ceil(parseFloat(str) / 10) * 10+'%';
})
<span class="tooltip">
<span class="grade">83.49%</span>
<span class="grade">83.49%</span>
<span class="grade">43.49%</span>
</span>
You can also use separate ids for elements to call them by id as follows;
function cha()
{
ele=document.getElementById("grade");
ele.innerHTML="90%";
}
<span class="tooltip">
<span id="grade">83.49%</span>
</span>
<button onclick="cha()">Change</button>
Edit: If you still want to use classes then you can use 0th index of class array you got by using getElementsByClassName follows;
function cha()
{
ele=document.getElementsByClassName("grade");
ele[0].innerHTML="90%";
}
<span class="tooltip">
<span class="grade">83.49%</span>
</span>
<button onclick="cha()">Change</button>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have added an id to all elements with a class of image using JS.
The images now have ids eg. image--1, image--2, etc
If I want to get the element by ID using var el = document.getElementById('image--1'); then I get null.
How can I get current DOM in pure JS?
You can use the setAttribute() method to reliably set the id of image elements, via vanillajs.
Something to also keep in mind here is that the image elements need to be defined and present in the DOM before your javascript executes.
With these two things in mind, consider the following code - you could do something along these lines to achieve what you're after;
<html>
<head>
</head>
<body>
<!-- DEFINE BEFORE SCRIPT -->
<img class="img" src="/yourimage0.jpg" />
<img class="img" src="/yourimage1.jpg" />
<!-- DEFINE SCRIPT AFTER IMAGE ELEMENTS -->
<script>
var id = 1;
// Use the querySelectorAll method to select collection of your
// image elements
for(var img of document.querySelectorAll(".img")) {
// Set the id attribute in this way, using the setAttribute method
img.setAttribute("id", "image--" + id);
id++;
}
// You can now access the element by id with the following
var el = document.getElementById('image--1');
</script>
</body>
</html>
This works. Please explain where your code differs
document.querySelectorAll(".img").forEach(function(img,i) {
img.id="image--"+i;
})
document.getElementById('image--1').src = "https://via.placeholder.com/350x150?text=Image1";
<img class="img" src="https://via.placeholder.com/350x150" />
<img class="img" src="https://via.placeholder.com/350x150" />
<img class="img" src="https://via.placeholder.com/350x150" />
Dynamically:
// create the images:
var container = document.getElementById("container");
for (var i=0;i<3;i++) {
var img = document.createElement("img");
img.src="https://via.placeholder.com/350x150";
img.classList.add("img");
container.appendChild(img);
}
document.querySelectorAll(".img").forEach(function(img,i) {
img.id="image--"+i; // makes more sense to do that in the creation part too
})
document.getElementById('image--1').src = "https://via.placeholder.com/350x150?text=Image1";
<div id="container"></div>
I guess you are using web frameworks like Vue.js or React.js, then this is because of virtual DOM, which means you can't get these elements before they are mounted to real DOM. If so, you should get these element in componentDidMount function.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I believe js shoulod support:
<script>
text = "This Title";
document.write( text.heading(1));
</script>
but it is not so. Any correction or explanatioin?
Why do you do .heading(1) ??
<script>
text = "This Title";
document.write(text);
</script>
Or you are trying to change title?
document.title = text;
Or you want to add text to particular place on the webpage?
<div id="heading1">
</div>
<div id="heading2">
</div>
<script>
text = "This Title";
text2 = "This is not Title";
document.getElementById('heading1').innerHTML = text;
document.getElementById('heading2').innerHTML = text2;
</script>
I guess you're trying to write <h1>This Title</h1> but there is no heading method in String prototype.
HTML Tag Methods (http://msdn.microsoft.com/en-us/library/ie/ff806183%28v=vs.94%29.aspx) may be a bit confusing cause javascript supports just few of html tags.
If you really need to make String.heading work, try this:
String.prototype.heading = function(level) {
return '<h' + level + '>' + this + '</h' + level + '>';
};
Note: extending prototype of built in objects is generally bad idea.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
<div class="main">
<textarea rows ="20" cols="80" name ="output_box" id ="output"></textarea>
</div>
What I want it to do is to add text to that area on a button click like so
<div class="classname" button type =onclick="myFunction()" >
Export
</div>
and this is what it calls
<script>
function myFunction()
{
var obj = document.getElementById("output").innerHTML;
var text = document.createTextNode("Test data");
obj.innerHTML = text;
}
</script>
But after much frustration I cannot figure it out.
Example with the changes below: http://jsfiddle.net/charlescarver/hZw6q/
Your JS should be closer to this:
var obj = document.getElementById("output");
var txt = "Test data";
obj.value = txt;
txt != text
As Matt Ball pointed out, "obj is a string," not an object.
You don't need document.createTextNode as you're using value instead of innerHtml
Your HTML should also be:
<div class="classname" type="button" onClick="myFunction()">
Export
</div>
And not:
<div class="classname" button type =onclick="myFunction()" >
Export
</div>