Cannot change image src on click
I am trying to change the source by clicking on the anchor. This is what I have tried so far:
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
JS:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'img/phones/2.png');
})
});
Nothing happens when I click on my link.
javascript jquery html
add a comment |
I am trying to change the source by clicking on the anchor. This is what I have tried so far:
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
JS:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'img/phones/2.png');
})
});
Nothing happens when I click on my link.
javascript jquery html
add a comment |
I am trying to change the source by clicking on the anchor. This is what I have tried so far:
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
JS:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'img/phones/2.png');
})
});
Nothing happens when I click on my link.
javascript jquery html
I am trying to change the source by clicking on the anchor. This is what I have tried so far:
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
JS:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'img/phones/2.png');
})
});
Nothing happens when I click on my link.
javascript jquery html
javascript jquery html
edited Nov 15 '18 at 9:55
waddupxnxxx
4812
4812
asked Nov 15 '18 at 9:45
DesiignerDesiigner
37419
37419
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
It isn't working as your HTML structure is invalid. You have an additional closing </div>
tag at the very bottom. Remove this and it will work for you.
See working example below:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
<!-- </div> <--- Remove this -->
add a comment |
You just need to check closing tag of </div>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
add a comment |
$("a#popularity").on( "click", function() {
$('.img_main').attr('src', 'https://images.unsplash.com/photo-1542256015-e6bb100b3f4c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=61c4ef1293dde4a044a0a3efa825e17e&auto=format&fit=crop&w=500&q=60');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="https://images.unsplash.com/photo-1542223616-740d5dff7f56?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1887f4d330d6bede705000e6ed7ba193&auto=format&fit=crop&w=500&q=60" alt="screenshot from iOS">
</div>
add a comment |
As everybody is pointing out, the invalid HTML structure is what causes this issue. Normally, browsers fix up bad HTML as best they can. In this case, you end up with HTML like:
<div class="main">
<a id="popularity" href="#"></a><div class="banner-part l_banner_efs"><a id="popularity" href="#">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
Note that the anchor is split into two elements, both with the same id
. id
s must be unique. When they aren't, and you select by id
, you usually only get the first element with that id
. Your javascript, $("#popularity")
, selects that first anchor to which you then attach a click handler. But that anchor doesn't contain anything, collapses to nothing, and so can't be normally interacted with. All the clicking the user does happens on the second anchor which doesn't have a click handler associated with it.
Of course, fixing the HTML structure will fix this issue.
add a comment |
There are unbalanced tag in you html. The closing div
tag after closing a
need to before closing a
like this
</div>
</a>
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Aspen-PopulusTremuloides-2001-09-27.jpg/220px-Aspen-PopulusTremuloides-2001-09-27.jpg');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Larix_decidua_Aletschwald.jpg/220px-Larix_decidua_Aletschwald.jpg" alt="screenshot from iOS">
</div>
</div>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316534%2fcannot-change-image-src-on-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
It isn't working as your HTML structure is invalid. You have an additional closing </div>
tag at the very bottom. Remove this and it will work for you.
See working example below:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
<!-- </div> <--- Remove this -->
add a comment |
It isn't working as your HTML structure is invalid. You have an additional closing </div>
tag at the very bottom. Remove this and it will work for you.
See working example below:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
<!-- </div> <--- Remove this -->
add a comment |
It isn't working as your HTML structure is invalid. You have an additional closing </div>
tag at the very bottom. Remove this and it will work for you.
See working example below:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
<!-- </div> <--- Remove this -->
It isn't working as your HTML structure is invalid. You have an additional closing </div>
tag at the very bottom. Remove this and it will work for you.
See working example below:
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
<!-- </div> <--- Remove this -->
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
<!-- </div> <--- Remove this -->
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
<!-- </div> <--- Remove this -->
answered Nov 15 '18 at 9:51
Nick ParsonsNick Parsons
9,0232824
9,0232824
add a comment |
add a comment |
You just need to check closing tag of </div>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
add a comment |
You just need to check closing tag of </div>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
add a comment |
You just need to check closing tag of </div>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
You just need to check closing tag of </div>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
answered Nov 15 '18 at 9:54
Aquib IqbalAquib Iqbal
21417
21417
add a comment |
add a comment |
$("a#popularity").on( "click", function() {
$('.img_main').attr('src', 'https://images.unsplash.com/photo-1542256015-e6bb100b3f4c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=61c4ef1293dde4a044a0a3efa825e17e&auto=format&fit=crop&w=500&q=60');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="https://images.unsplash.com/photo-1542223616-740d5dff7f56?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1887f4d330d6bede705000e6ed7ba193&auto=format&fit=crop&w=500&q=60" alt="screenshot from iOS">
</div>
add a comment |
$("a#popularity").on( "click", function() {
$('.img_main').attr('src', 'https://images.unsplash.com/photo-1542256015-e6bb100b3f4c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=61c4ef1293dde4a044a0a3efa825e17e&auto=format&fit=crop&w=500&q=60');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="https://images.unsplash.com/photo-1542223616-740d5dff7f56?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1887f4d330d6bede705000e6ed7ba193&auto=format&fit=crop&w=500&q=60" alt="screenshot from iOS">
</div>
add a comment |
$("a#popularity").on( "click", function() {
$('.img_main').attr('src', 'https://images.unsplash.com/photo-1542256015-e6bb100b3f4c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=61c4ef1293dde4a044a0a3efa825e17e&auto=format&fit=crop&w=500&q=60');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="https://images.unsplash.com/photo-1542223616-740d5dff7f56?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1887f4d330d6bede705000e6ed7ba193&auto=format&fit=crop&w=500&q=60" alt="screenshot from iOS">
</div>
$("a#popularity").on( "click", function() {
$('.img_main').attr('src', 'https://images.unsplash.com/photo-1542256015-e6bb100b3f4c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=61c4ef1293dde4a044a0a3efa825e17e&auto=format&fit=crop&w=500&q=60');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="https://images.unsplash.com/photo-1542223616-740d5dff7f56?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1887f4d330d6bede705000e6ed7ba193&auto=format&fit=crop&w=500&q=60" alt="screenshot from iOS">
</div>
$("a#popularity").on( "click", function() {
$('.img_main').attr('src', 'https://images.unsplash.com/photo-1542256015-e6bb100b3f4c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=61c4ef1293dde4a044a0a3efa825e17e&auto=format&fit=crop&w=500&q=60');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="https://images.unsplash.com/photo-1542223616-740d5dff7f56?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1887f4d330d6bede705000e6ed7ba193&auto=format&fit=crop&w=500&q=60" alt="screenshot from iOS">
</div>
$("a#popularity").on( "click", function() {
$('.img_main').attr('src', 'https://images.unsplash.com/photo-1542256015-e6bb100b3f4c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=61c4ef1293dde4a044a0a3efa825e17e&auto=format&fit=crop&w=500&q=60');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#"><div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a>
</div>
<div class="phone_banner Column">
<img class="img_main" src="https://images.unsplash.com/photo-1542223616-740d5dff7f56?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1887f4d330d6bede705000e6ed7ba193&auto=format&fit=crop&w=500&q=60" alt="screenshot from iOS">
</div>
answered Nov 15 '18 at 9:50
svandewielsvandewiel
566
566
add a comment |
add a comment |
As everybody is pointing out, the invalid HTML structure is what causes this issue. Normally, browsers fix up bad HTML as best they can. In this case, you end up with HTML like:
<div class="main">
<a id="popularity" href="#"></a><div class="banner-part l_banner_efs"><a id="popularity" href="#">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
Note that the anchor is split into two elements, both with the same id
. id
s must be unique. When they aren't, and you select by id
, you usually only get the first element with that id
. Your javascript, $("#popularity")
, selects that first anchor to which you then attach a click handler. But that anchor doesn't contain anything, collapses to nothing, and so can't be normally interacted with. All the clicking the user does happens on the second anchor which doesn't have a click handler associated with it.
Of course, fixing the HTML structure will fix this issue.
add a comment |
As everybody is pointing out, the invalid HTML structure is what causes this issue. Normally, browsers fix up bad HTML as best they can. In this case, you end up with HTML like:
<div class="main">
<a id="popularity" href="#"></a><div class="banner-part l_banner_efs"><a id="popularity" href="#">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
Note that the anchor is split into two elements, both with the same id
. id
s must be unique. When they aren't, and you select by id
, you usually only get the first element with that id
. Your javascript, $("#popularity")
, selects that first anchor to which you then attach a click handler. But that anchor doesn't contain anything, collapses to nothing, and so can't be normally interacted with. All the clicking the user does happens on the second anchor which doesn't have a click handler associated with it.
Of course, fixing the HTML structure will fix this issue.
add a comment |
As everybody is pointing out, the invalid HTML structure is what causes this issue. Normally, browsers fix up bad HTML as best they can. In this case, you end up with HTML like:
<div class="main">
<a id="popularity" href="#"></a><div class="banner-part l_banner_efs"><a id="popularity" href="#">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
Note that the anchor is split into two elements, both with the same id
. id
s must be unique. When they aren't, and you select by id
, you usually only get the first element with that id
. Your javascript, $("#popularity")
, selects that first anchor to which you then attach a click handler. But that anchor doesn't contain anything, collapses to nothing, and so can't be normally interacted with. All the clicking the user does happens on the second anchor which doesn't have a click handler associated with it.
Of course, fixing the HTML structure will fix this issue.
As everybody is pointing out, the invalid HTML structure is what causes this issue. Normally, browsers fix up bad HTML as best they can. In this case, you end up with HTML like:
<div class="main">
<a id="popularity" href="#"></a><div class="banner-part l_banner_efs"><a id="popularity" href="#">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</a></div>
<div class="phone_banner Column">
<img class="img_main" src="img/phones/1.png" alt="screenshot from iOS">
</div>
</div>
Note that the anchor is split into two elements, both with the same id
. id
s must be unique. When they aren't, and you select by id
, you usually only get the first element with that id
. Your javascript, $("#popularity")
, selects that first anchor to which you then attach a click handler. But that anchor doesn't contain anything, collapses to nothing, and so can't be normally interacted with. All the clicking the user does happens on the second anchor which doesn't have a click handler associated with it.
Of course, fixing the HTML structure will fix this issue.
edited Nov 15 '18 at 10:17
answered Nov 15 '18 at 10:09
OuroborusOuroborus
6,7511535
6,7511535
add a comment |
add a comment |
There are unbalanced tag in you html. The closing div
tag after closing a
need to before closing a
like this
</div>
</a>
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Aspen-PopulusTremuloides-2001-09-27.jpg/220px-Aspen-PopulusTremuloides-2001-09-27.jpg');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Larix_decidua_Aletschwald.jpg/220px-Larix_decidua_Aletschwald.jpg" alt="screenshot from iOS">
</div>
</div>
add a comment |
There are unbalanced tag in you html. The closing div
tag after closing a
need to before closing a
like this
</div>
</a>
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Aspen-PopulusTremuloides-2001-09-27.jpg/220px-Aspen-PopulusTremuloides-2001-09-27.jpg');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Larix_decidua_Aletschwald.jpg/220px-Larix_decidua_Aletschwald.jpg" alt="screenshot from iOS">
</div>
</div>
add a comment |
There are unbalanced tag in you html. The closing div
tag after closing a
need to before closing a
like this
</div>
</a>
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Aspen-PopulusTremuloides-2001-09-27.jpg/220px-Aspen-PopulusTremuloides-2001-09-27.jpg');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Larix_decidua_Aletschwald.jpg/220px-Larix_decidua_Aletschwald.jpg" alt="screenshot from iOS">
</div>
</div>
There are unbalanced tag in you html. The closing div
tag after closing a
need to before closing a
like this
</div>
</a>
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Aspen-PopulusTremuloides-2001-09-27.jpg/220px-Aspen-PopulusTremuloides-2001-09-27.jpg');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Larix_decidua_Aletschwald.jpg/220px-Larix_decidua_Aletschwald.jpg" alt="screenshot from iOS">
</div>
</div>
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Aspen-PopulusTremuloides-2001-09-27.jpg/220px-Aspen-PopulusTremuloides-2001-09-27.jpg');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Larix_decidua_Aletschwald.jpg/220px-Larix_decidua_Aletschwald.jpg" alt="screenshot from iOS">
</div>
</div>
$(document).ready(function() {
$("#popularity").click(function() {
$('.img_main').attr('src', 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Aspen-PopulusTremuloides-2001-09-27.jpg/220px-Aspen-PopulusTremuloides-2001-09-27.jpg');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<a id="popularity" href="#">
<div class="banner-part l_banner_efs">
<img class="banner-logo" src="img/logos/popularity.png" alt="Popularity">
<h3 class="banner-header">POPULARITY<br/>MESSAGING</h3>
<p class="banner-text">Dynamic audience size and sales counters</p>
</div>
</a>
<div class="phone_banner Column">
<img class="img_main" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Larix_decidua_Aletschwald.jpg/220px-Larix_decidua_Aletschwald.jpg" alt="screenshot from iOS">
</div>
</div>
answered Nov 15 '18 at 9:54
brkbrk
27.7k32141
27.7k32141
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316534%2fcannot-change-image-src-on-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown