Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How do I modify my code to operate with multiple tags with the same id?

JavaScript:
<style>
.mapcontent {
    position: relative;
    z-index: 10;
    width: 400px;
    height: 400px;
    overflow:hidden;
    background-image: url(stars.jpg);
}
#pointer {
    position: absolute;
    top: 0;
    left: 0;
    width: 30px;
    height: 30px;
    background-image: url(sto-pointer-icon-tx.png);
    z-index: 50;
    animation: move 3s ease;
}
 @keyframes move {
 50% {
 top: 400px;
 left: 400px;
}
}
}
</style>
</head>
<body>
<div class="container">
  <div class="row">
    <div class="col" id="wrap">
      <table>
        <thead>
          <tr>
            <th width="100">System List</th>
            <th width="25">X</th>
            <th width="25">Y</th>
          </tr>
        </thead>
        <tbody>
          <tr>
<td >
<a href='#' id='systemname' class='systemname'>acamar</a>
</td>
<td id='ix'>15</td><td id='iy'>80</td></tr>
<tr>
<td >
<a href='#' id='systemname' class='systemname'>bajor</a>
</td>
<td id='ix'>60</td><td id='iy'>75</td></tr>
<tr>
<td >
<a href='#' id='systemname' class='systemname'>sol</a>
</td>
<td id='ix'>25</td><td id='iy'>25</td></tr>
<tr>
<td >
<a href='#' id='systemname' class='systemname'>vulcan</a>
</td>
<td id='ix'>50</td><td id='iy'>40</td></tr>
        </tbody>
        <tfoot>
          <tr>
            <th colspan="3">--</th>
          </tr>
        </tfoot>
      </table>
    </div>
    <div class="col mapcontent">
      <div id="pointer"></div>
    </div>
  </div>
</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="../js/jquery-3.6.0.js"></script>

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="../js/bootstrap.js"></script>


<script language="javascript">
$(document).ready(function() {
    $("#systemname").on('click', function(event) {
            var xx = document.getElementById("ix").innerHTML;
         var yy = document.getElementById("iy").innerHTML;
         document.getElementById('pointer').style.left = xx+'px';
         document.getElementById('pointer').style.top = yy+'px';
       event.preventDefault();
    });
});
</script>       
</body>
</html>
 
You can't have more then one element with the same id. That's what an id is for, to be unique. You'll have to use class instead.

You're using both regular JS and jQuery? $("#systemname") and document.getElementById('pointer')

I'm assuming that you're using jQuery, so use $('.classname') instead of $('#id') if you use a class name instead.
 
Thanks for your reply Johna, I remove the 'id' tags from the list and changed the script to point at the class but all links move the graphic to the first set of coords, would you know if its possible to read data-id from the link using jQuery? or pass the values of ix and iy thru to the Jquery code?
 
If you want to use jQuery, you could do the following to accomplish the task:

JavaScript:
$(document).ready(function () {
  $(".systemname").on("click", function (event) {
    event.preventDefault();
    var $row = $(this).parent().parent();
    var x = $row.children().eq(1).text() + "px";
    var y = $row.children().eq(1).text() + "px";
    $("#pointer").css({"left": x, "top": y});
  });
});
 
Corrected code below:

JavaScript:
$(document).ready(function () {
  $(".systemname").on("click", function (event) {
    event.preventDefault();
    var $row = $(this).parent().parent();
    var x = $row.children().eq(1).text() + "px";
    var y = $row.children().eq(2).text() + "px";
    $("#pointer").css({"left": x, "top": y});
  });
});
 
Corrected code below:

JavaScript:
$(document).ready(function () {
  $(".systemname").on("click", function (event) {
    event.preventDefault();
    var $row = $(this).parent().parent();
    var x = $row.children().eq(1).text() + "px";
    var y = $row.children().eq(2).text() + "px";
    $("#pointer").css({"left": x, "top": y});
  });
});
Thanks Randell, that works a treat, however, I have a CSS ease transition when pointer first apprears (slides to initial coords) is there a way to modify the code so that the pointer 'eases' from point to point?
 
Sure, when my pointer is in one position (current set of coords) when user clicks on another system, the pointer should CSS animate to the selected coords, initially when the page loads i have the following css to check its working.
Code:
#pointer {
    position: absolute;
    top: 0;
    left: 0;
    width: 30px;
    height: 30px;
    background-image: url(pointer.gif);
    z-index: 50;
    animation: move 3s ease;
}
 @keyframes move {
 50% {
 top: 400px;
 left: 400px;
}
}

now when the user clicks on new option (new coords) the pointer should move/transition to new position rather than moving instantly
 
Try adding transition to the pointer css.
CSS:
#pointer {
  position: absolute;
  top: 0;
  left: 0;
  width: 30px;
  height: 30px;
  background-image: url(pointer.gif);
  z-index: 50;
  animation: move 3s ease;
  transition: 1s; /* transition */
}

@keyframes move {
  50% {
    top: 400px;
    left: 400px;
  }
}
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom