I am trying to figure out why the iOS Safari browser won't apply a href
alteration, like it does on a desktop browser. So far, I have tried .attr()
and .prop()
, but neither one seems to apply the URL adjustment.
To be more descriptive, I am trying to replace the href
location of the last linked element in a row of 4 elements.
As of now, the generated HTML appears something like this:
<a href="http://www.URL.com/link-to-img-url-1">[LINK TO IMAGE 1]</a>
<a href="http://www.URL.com/link-to-img-url-2">[LINK TO IMAGE 2]</a>
<a href="http://www.URL.com/link-to-img-url-3">[LINK TO IMAGE 3]</a>
<a href="http://www.URL.com/link-to-img-url-4">[LINK TO IMAGE 4]</a>
But I would like for the fourth link to have a different URL applied, like so:
<a href="http://www.DIFFERENTURL.com/">[LINK TO IMAGE 4]</a>
Here is a full snippet example (the image links are just placeholders):
$(document).ready(function() {
$("a:last").attr("href", "http://www.google.com/");
});
a {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="http://www.URL.com/link-to-img-url-1">[LINK TO IMAGE 1]</a>
<a href="http://www.URL.com/link-to-img-url-2">[LINK TO IMAGE 2]</a>
<a href="http://www.URL.com/link-to-img-url-3">[LINK TO IMAGE 3]</a>
<a href="http://www.URL.com/link-to-img-url-4">[LINK TO IMAGE 4]</a>
And these are some other scripts I've tried implementing, so far:
With .attr() / NOT working on iOS:
$(document).ready(function(){
$("#instafeed a:nth-child(4)").attr("href", "http://www.DIFFERENTURL.com/");
});
With .prop() / NOT working on iOS:
$(document).ready(function(){
$("#instafeed a:nth-child(4)").prop("href", "http://www.DIFFERENTURL.com/");
});
With :eq(3) + .attr() / NOT working on iOS:
$(document).ready(function(){
$("#instafeed a:eq(3)").attr("href", "http://www.DIFFERENTURL.com/");
});
With [0].href / NOT working on iOS:
$(document).ready(function(){
$('#instafeed a:last')[0].href = 'http://www.DIFFERENTURL.com/';
});
With replaceWith (Method 1) / NOT working on iOS
$(document).ready(function(){
$('#instafeed a').last().replaceWith('http://www.DIFFERENTURL.com/');
});
With replaceWith (Method 2) / NOT working on iOS:
$(document).ready(function() {
$("#instafeed a:last").show(function() {
var before = $(this).attr('href');
var replacewith = "https://DIFFERENTURL.com/";
var after = before.replace(before, replacewith);
$(this).attr("href", after);
});
});