When two texts are enclosed in p tags, my goal is to create an interactive function where clicking on one text causes it to move slightly and the other text to be temporarily deleted. However, when the deleted text is re-added after clicking on the first text again, the mousedown function stops working. It's worth noting that the mousedown event only works on the initially clicked text/tag upon page load, making it the only clickable element.
$(document).ready(function() {
var upPoBool = false;
//first part
$('#firstPart').mousedown(function() {
if(upPoBool == false){
var firstPart = document.getElementById("firstPart");
firstPart.classList.toggle("firstPartToggle");
//delete second part
$("#secondPart").remove();
upPoBool = true;
} else if(upPoBool == true){
var firstPart = document.getElementById("firstPart");
firstPart.classList.toggle("firstPartToggle");
$('#firstPart:last').after('<p id="secondPart">Second part move NEW</p>');
upPoBool = false;
}
});
var upBaBool = false;
//second part
$('#secondPart').mousedown(function() {
if(upBaBool == false){
var secondPart = document.getElementById("secondPart");
secondPart.classList.toggle("secondPartToggle");
//delete first part
$("#firstPart").remove();
upBaBool = true;
} else if(upBaBool == true){
var secondPart = document.getElementById("secondPart");
secondPart.classList.toggle("secondPartToggle");
$('#secondPart:last').before('<p id="firstPart">First part move NEW</p>');
upBaBool = false;
}
});
});
.firstPartToggle{
margin-left: 5rem;
}
.secondPartToggle{
margin-left: 5rem;
}
#firstPart{
position: absolute;
top: 2rem;
z-index: 101;
}
#secondPart{
position: absolute;
top: 4rem;
z-index: 100;
}
<html>
<head>
<meta charset="UTF-8">
<title>ClickTestFAQ</title>
<link rel="stylesheet" href="style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<p>Something test:</p>
<div id="testingDiv">
<p id="firstPart">First part move</p>
<p id="secondPart">Second part move</p>
</div>
<!--Scripts-->
<script src="jquery.js"></script>
</body>
</html>
The tags do not overlap, so adjusting the z-index does not have any effect. I even tried placing them in separate divs but encountered the same issue.