Get image alt using JavaScript [duplicate] - javascript

This question already has answers here:
Get all Attributes from a HTML element with Javascript/jQuery
(20 answers)
Closed 2 years ago.
I have
<img alt="text" src="source" />
I'd like to get this alt to one var in JavaScript. It'll be better if it won't be necessary to use jQuery.
Please for help :)

You could do like this:
document.getElementById('imgId').getAttribute('alt')

can get any attribute by .getAttribute
i.e.
imgAlt.getAttribute("alt")
imgAlt.getAttribute("title")
const imgAlt = document.getElementById("myImg");
console.log(imgAlt.getAttribute("alt"));
<img id="myImg" alt="text" src="source" />

Related

How to make the links in Javascript code nofollow [duplicate]

This question already has answers here:
window.open() add a rel="nofollow" attribute
(3 answers)
Closed 5 years ago.
I want to make the links in this javascript code nofollow. Please help me how to do that.
<img src="https://example.png/>
According to https://developer.mozilla.org/en-US/docs/Web/API/Window/open you can add a third parameter in the window.open method that is a comma separated string with name value pairs - but rel is not supported...
Seems like html may be a good option.
<a href="www.example.com" rel="nofollow" target="_blank" >Link</a>
If you need to do this from js you could trigger a click on the link.

Add URL to href tag with javascript [duplicate]

This question already has answers here:
How to change href of <a> tag on button click through javascript
(8 answers)
Closed 6 years ago.
How can I add a Url to the href tag with javascript? Can you help, please?
<a id="url-link" href="" onclick="" title="" class="btn">CLICK</a>
https://jsfiddle.net
Try this
var a = document.getElementById('url-link');
a.href="your link here";

How to output img to a child class [duplicate]

This question already has answers here:
Programmatically change the src of an img tag
(9 answers)
Closed 6 years ago.
<div class="fruit" id="whole">
<img class="fruit1" />
<img class="fruit2" />
</div>
<script>
var img1=document.createElement("img");
img1.src="http://us.yimg.com/i/us/nws/weather/gr/32ds.png";
document.getElementById("whole").appendChild(img1);
</script>
I only know how to add <img> tag in the parent class, and output a image in it, like the code does.
However, I don't know how to output a image to the child class, i.e,"fruit1"
Are you trying to set the img src="" of .fruit1? If so you just need to set the src property after finding it
var fruit1image = document.getElementsByClassName("fruit1")[0];
fruit1image.src="http://us.yimg.com/i/us/nws/weather/gr/32ds.png";
Note that getElementsByClassName returns an array, so we need to grab the first (0th) element.

A simple find and replace function in either JavaScript or JQuery [duplicate]

This question already has answers here:
How to change the href attribute for a hyperlink using jQuery
(13 answers)
Closed 8 years ago.
I was wondering if anyone knew a way I would be able to find and replace all prefixes for the src of certain image files, e.g. on button click -> find all img src="Images/g/....." and replace with img src="Images/r/....."
<table>
<tr><td>
<img src="Images/jpg/g/tblLogo/fcs6.jpg" height="30" width="30">
</td><td>
<img src="Images/jpg/g/tblLogo/dcs6.jpg" height="30" width="30">
</td></tr>
<table>
As you can see from the above extract, my files are saved within a folder named 'g', I need a function that will scan my entire HTML files and change all those src's to point to the same file but within "Images/jpg/r/....."
P.S. Simplicity is the key.
While I question the use case as such, this would be a way to go about it:
$('img[src^="Images/jpg/g/"]').each(function() {
this.src = this.src.replace(/^Images\/jpg\/g\//, 'Images/jpg/r/');
});

jQuery: replace text inside link? [duplicate]

This question already has answers here:
change html text from link with jquery
(7 answers)
Closed 7 years ago.
I have the following html structure:
<div class="event tiny">
Something
</div>
And I want to replace the text of this link with "Anything" …
When doing this …
$('.event a').replaceWith("Anything");
… the text is replaced but also the link is gone.
When doing this … 
$('.event a').text().replaceWith("Anything");
… nothing happens.
Like this, if the new text is all the same.
$('.event a').text("Anything");
jQuery loops over the a elements, and replaces its content with the "Anything" text.
These suggestions, for setting the inner text & URL link within a a href didn't work for me (using JQuery v1.8.3)
Here's what did work though:
HTML
<a id="hrefAnalysisName" ></a>
JavaScript
$("a#hrefAnalysisName").attr("href", "/SomePage/ShowAnalysisDetails.aspx");
$("a#hrefAnalysisName").text("Show analysis");
Hope this helps!
You could also change the link by href if it's related to the URL.
$('a[href="/whatever"]').text("Anything");
You are able to get this result the next ways result will be the same:
HTML
<div class="event tiny">
<a class="html" href="/whatever">Something</a></br>
<a class="text" href="/whatever">Something</a></br>
<a class="replaceWith" href="/whatever">Something</a>
</div>
JS
$('.event a.html').html('Anything-html')
$('.event a.text').text('Anything-text')
$('.event a.replaceWith').replaceWith('<a class="replaceWith" href="/whatever">Anything-replaceWith</a>')

Categories