0
Good people, I have a problem here. I searched here and on the Internet several times and I did not have an answer that would help me enough, I still have doubts.
I have a website and I have "Collapsible" sectors, so to illustrate, I would like to put an arrow pointed up and when I clicked, pointed down, or whatever. I was able to do this, however, just once, if I click again, the image stays down and does not change any more until I refresh the page. I put the image at the beginning of the text, below I have an example.
In fact, what I really wanted was for her to change when I clicked on any part of the title of the "Collapsible" item, but for now I only got her to change when I clicked on it.
Anyway, that’s two questions in the case. How do I get her to stay in "loop" and every time I click she changes? and How do I get it to click on the title as well??
I put the javascript code together to facilitate my tests, the idea is to move to another file later. I’m a beginner, I have experience with HTML and CSS, but javascript is very basic, I’m remembering a lot of HTML and CSS with this project that has not been moving for a long time and would like a jauda. Thank you very much!
HTML with Javascript code together:
<div class="card">
<div class="card-header" role="tab" id="headingTwo">
<h5 class="mb-0">
<h1 class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" ng-click="alert_step2()">
<img id="example" onclick="changeImage()" src="assets/img/arrow_up.png" width="20" height="25">
<script>
function changeImage(){
element=document.getElementById('example')
if (element.src.match("out")){
element.src="assets/img/arrow_up.png";
}
else{
element.src="assets/img/arrow_down.png";
}
changeImage.repeat(100);
}
</script>
Step 2 - Acknowledge Your Strengths (highest scores)
</h1>
</h5>
</div>
</div>
<div class="card">
<div class="card-header" role="tab" id="headingTwo">
<h5 class="mb-0">
<h1 class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" ng-click="alert_step2()">
<h1 class="change"><img class="change img-change" src="assets/img/arrow_up.png" style="width: 20px; height: 25px">Step 2 - Acknowledge Your Strengths (highest scores)</h1>
Remembering that the script that Mateus Veloso gave me is under that Ollapse
Updated code:
<div class="card">
<div class="card-header" role="tab" id="headingTwo">
<h5 class="mb-0">
<script>
$('.change').click((e) => {
var img1 = 'assets/img/arrow_up.png';
var img2 = 'assets/img/arrow_down.png';
var element = $('.img-change');
if(element.attr('src') === img1){
element.attr('src',img2);
}else if(element.attr('src') === img2){
element.attr('src',img1);
}
});
</script>
<h1 class="collapsed change" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" ng-click="alert_step2()"> <img class="change img-change" src="assets/img/arrow_up.png" style="width: 20px; height: 25px">Step 2 - Acknowledge Your Strengths (highest scores)</h1>
</h1>
</h5>
</div>
I think he wants it with pure Javascript.
– LeAndrade
@Leandrade I’ll create another example then!
– Mateus Veloso
Done @Leandrade
– Mateus Veloso
I have another question Matthew, first of all Thank you! Second, I put in the code and made the necessary changes, however, Collapse no longer opens, is working the image exchange, but does not expand and shows the hidden information more, how can I solve?
– EduardoBacarin
@Eduardobacarin remembering that your Collapse should continue with all tags before, the ideal would be to see how is this your code snippet, post your current Collapse!
– Mateus Veloso
<H1 class="collapsed" data-toggle="Collapse" data-Parent="#accordion" href="#collapseTwo" Aria-Expanded="false" Aria-Controls="collapseTwo" ng-click="alert_step2()"> the class and all attributes should be maintained in that case
– Mateus Veloso
I’ll post the Collapse code here.
– EduardoBacarin
It’s done Matthew, if you can take a look, I’d appreciate it. I didn’t modify it, I just put in the script and it stopped working
– EduardoBacarin
@Eduardobacarin hello, delete <H1> <H1 class="change"><img class="change img-change" src="Assets/img/arrow_up.png" style=": 20px width; height: 25px">Step 2 - Acknowledge Your Strengths (Highest Scores)</H1> and add the class change in the above Collapse, thus staying : <H1 class="collapsed change" data-toggle="Collapse" data-Parent="#accordion" href="#collapseTwo" Aria-Expanded="false" Aria-Controls="collapseTwo" ng-click="alert_step2()">
– Mateus Veloso
The problem is that Voce created a new H1 you understand ? your old one that gets the click to expand its content, is simply copy everything from the top or delete the bottom one!
– Mateus Veloso
I just copied what you posted and pasted, and it’s not working, I think I’m missing something simpler, like call the JS file or something, I’m really breaking my head with this... I don’t know how to put it all together :/
– EduardoBacarin
Calm young make the change I asked you that will work
– Mateus Veloso
Do just that, <H1 class="collapsed change" data-toggle="Collapse" data-Parent="#accordion" href="#collapseTwo" Aria-Expanded="false" Aria-Controls="collapseTwo" ng-click="alert_step2()"> <img class="change img-change" src="Assets/img/arrow_up.png" style="width: 20px; height: 25px">Step 2 - Acknowledge Your Strengths (Highest Scores)</H1>
– Mateus Veloso
Ta ai the code that will put in place, realize that I deleted the <H1 that Voce added and pasted the variable change on H1 that already existed , understood ?
– Mateus Veloso
I think I did the way you said, Matthew. Forgive the hardship hahaha, I’m trying to learn right yet... I put the code up to date, the way you talked to me, copied it and replaced it. Look there please. I even put the script there so you can see. What’s wrong? The arrow appears, the Collapse works, but it doesn’t change the image.
– EduardoBacarin
No problem @Eduardobacarin , the <script> tag has to be below , the problem is this , is probably generating a javascript error because at the time of execution of your script the element has not yet been created on the page, got it ? play <script>...tag. </script> there end of page that will work
– Mateus Veloso
Dear heaven! You helped me with a problem that I was here all day trying! Thank you so much indeed! Thank you for your patience and tips! A huge hug for you!
– EduardoBacarin
If my answer helped you, evaluate it with the green haha, thank you! @Eduardobacarin
– Mateus Veloso
I don’t know how to work the platform much yet, but I guess it was, right?
– EduardoBacarin
It worked yes haha, thank you! needing ....
– Mateus Veloso
Matthew, I’m back! hahahha I had a little problem. I have to do this in 12 collapses, when I went by in the second, when testing, when I click on the second collapse, the arrow image of the first exchange together, that is, the two photos exchange together regardless of which I click, what can I do? I changed the collapsetwo to trhee, and the alert_step2 to alert_step3, nothing changed.
– EduardoBacarin
This would be a topic for another question, how to do this in a functional and simple way, ask the question and send the link here, I will go there on time and create the code to help you!
– Mateus Veloso
https://answall.com/questions/339020/imagem-collapse-abrindo-duplicada ta Mateus, thanks!
– EduardoBacarin