Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have the following html code:
<div class=imgHolder>
<img src="some address"/>
<a class="del" onClick="function">delete</a>
</div>
how find image tag in the parent's of by pure java script
Javascript:
function clickFunc(e){
var tgt = e.target;
var parent = tgt.parentNode;
var img = parent.getElementsByTagName("img")[0];
parent.removeChild(img);
}
HTML:
<div class=imgHolder>
<img src="some address"/>
<a class="del" onClick="clickFunc">delete</a>
</div>
I tried to write it as self explanatory, but a walk through:
e is the click event.
e.target is what the user clicked on (Your anchor tag)
parent is the parent node of the anchor.
img is the first image in the parent node.
Remove img from parent.
<div class=imgHolder>
<img src="some address"/>
delete
</div>
Script:
function deleteThis(sender){
var childs = sender.parentNode.childNodes;
for (var i = 0; i < childs.length; i++){
if (childs[i].tagName === 'img')
alert(childs[i].src);
}
}
OR, if you're sure img will be always before link.
function deleteThis(sender){
if (sender.previousSibling.tagName === 'img')
alert(sender.previousSibling.src);
}
Related
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 3 days ago.
Improve this question
I am using chrome devtools to inspect html. I have this code where I apply querySelector for inner elements:
var x = document.querySelectorAll(".search__results-list li");
var myarray = []
for (var i=0; i<x.length; i++){
const href = x[i].querySelector('.ahchor[href]');
const text = x[i].querySelector('.text-field');
myarray.push([href, text]);
};
The querySelector does not exists on x[i], getting an error Uncaught TypeError: [...].querySelector is not a function how to fix that?
here is the html sample, in reality there is a lot of li's wthin:
<ul class="search__results-list">
<li class="search__result-container">
<div class="search-result">
<div class="search-result__item">
<div class="search-result_block">
<div class="center">
<a class="ahchor" href="somesite.com/a">
<div>
<div class="wrapper">
<div class="entity">
<div class="text-field">content here</div>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
</div>
</li>
</ul>
In your code syntax errors
Should be:
var x = document.querySelectorAll(".search__results-list li");
var myarray = []
for (var i = 0; i < x.length; i++) {
const href = x[i].querySelector('.ahchor[href]');
const text = x[i].querySelector('.text-field');
myarray.push([href, text]);
};
This code should work if you wrote classes correctly and accordingly to your html
You can check this code even in console there, works fine. But I didn't find wrappers with 2 elements and use .m6 twice
enter image description here
I used the Chrome development tool to achieve this. I found that according to your code, you can achieve logic without error. You can run the code several times.
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 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 5 years ago.
Improve this question
I'd like to get all a Elements which got contain the classes menu-item-link and mk-image-link. Is the following expression right?
a[class*="menu-item-link"], a[class*="mk-image-link"]
Didn't get anything selected so I guess not :)
Thanks for the help.
Best regards,
Anton
You have to do this 'a.menu-item-link, a.mk-image-link' it will check if a has this class.
const selected = document.querySelectorAll('a.menu-item-link, a.mk-image-link')
const selected2 = document.querySelectorAll('a.menu-item-link.js-smooth-scroll, a.mk-image-link.js-smooth-scroll')
console.log(selected)
console.log(selected2)
<div class="menu-item-link"><div>
<div><div>
<a class="menu-item-link"></a>
<a class="mk-image-link"></a>
<a class="mk-image-link menu-item-link"></a>
<a class="menu-item-link js-smooth-scroll" href="/superfood-rezepte/">SUPERFOOD REZEPTE</a> <a href="...." target="_self" class="mk-image-link">
document.querySelectorAll allows grouping of selectors.
Note that, it would select all elements which has the specified class names, so a wild-card is not needed. i.e. if you are aiming at menu-item-link and mk-image-link the below should work for you.
So, you could try the below:
var elements = document.querySelectorAll("a[class~='menu-item-link'], a[class~='mk-image-link']"); // OR
elements = document.querySelectorAll("a.menu-item-link, a.mk-image-link"); // Would both selects the same
elements.forEach(function(element, index, array) {
element.style.backgroundColor = "#999";
});
<a class="js menu-item-link js-smooth-scroll" href="/superfood-rezepte/">SUPERFOOD REZEPTE</a> <br>
<a class="js menu-item-link js-smooth-scroll-a" href="/superfood-rezepte/">SUPERFOOD </a> <br>
<br>
Something
<br>
Another Something
<br>
Another Something
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am hoping to find out why the following code does not work:
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#myimg").attr("src", "image2.jpg");
});
</script>
<div>
<img class = "myimg" src = "image1.jpg" alt = "Sample image" />
</div>
while this block does:
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#myimg").attr("src", "image2.jpg");
});
</script>
<div>
<img id= "myimg" src = "image1.jpg" alt = "Sample image" />
</div>
In your html in the first block you need to set the id of the img tag to myimg:
<img id="myimg">
If you're going to call it with $('#myimg')
You can also just change your jquery to call it like:
$('.myimg')
That's messed up, but the error in your first example is you are looking for an element with an id #myimg instead of a class .myimg
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>