Retrieve the elements by their IDs in an svg where the ID equals '@data[]'

As I load data from SQL in a while loop to create my SVG, each record has its own ID. However, when attempting to retrieve the ID using getelementbyId, it consistently returns null values.

Below is the code snippet:

#!/usr/bin/perl
use DBI;
use CGI::Carp qw(fatalsToBrowser);
print "content-type: text/html\n\n";
print "Content-Type: image/svg-xml\n\n" ;
$dbh = DBI->connect ('dbi:Oracle:******','*****','*****')
|| die "database connection not made: $DBI::errstr";

$sth = $dbh->prepare("SELECT Find_id, xcoord, ycoord, gisteach.finds.type, gisteach.class.type, depth, name, period, use FROM GISTEACH.finds, GISTEACH.class where gisteach.finds.type = gisteach.class.type");
$sth->execute();

$sth1 = $dbh->prepare("SELECT lowx, hix, lowy, hiy, Field_id, owner, GISTEACH.FIELDS.crop, GISTEACH.crops.crop, name from GISTEACH.FIELDS, gisteach.crops where gisteach.crops.crop = gisteach.fields.crop");
$sth1->execute(); 

print qq(<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN"
  "http://www.w3.org/tr/2000/cr-svg-20001102/DTD/svg-20001102.dtd">);  
print qq(<svg width="20cm" height="20cm" viewBox="-1 -18 20 20" onload="getid(ID)">);

print qq (<script type="text/ecmascript">);
print qq (<![CDATA[  

  // Even with the 'onload' function, the issue persists.
  function getid(ID){ 
    (document.getElementById('ID'));
   }
  function MakeTransparent(evt) {
      evt.target.setAttributeNS(null,"opacity","0.5")
    }
  function MakeOpaque(evt) {
      evt.target.setAttributeNS(null,"opacity","1")
    }   

  function buttonClick(){
      var type = document.getElementById('ID');
      var data = type.getAttribute('d')
      var data2 = type.getAttribute('d2')
      var data3 = type.getAttribute('d3')     
      alert ("This Find can be placed in the: " + data2 + " age. In which its primary use was; " + data3 );
    }

    function buttonClick2() {  
      var type = document.getElementById('ID')             
      var data1 = type.getAttribute('b')
      var data2 = type.getAttribute('c')

      alert ("Owner of this field is: " + data1 + "  Where " + data2 + " are growing" );
    }
    ]]>);   
print qq (</script>);

while (@data = $sth1->fetchrow_array()) {

print qq(    
  <g transform="scale(1,-1)" > 
      <polygon points="$data[0],$data[2] $data[1],$data[2] $data[1],$data[3] $data[0],$data[3]" 
        fill="green"  
           ID="$data[4]"  
             b="$data[5]"
              c="$data[8]"
                opacity="1"
              stroke="black" 
            stroke-width="0.05"
         onmouseover="MakeTransparent(evt)" 
       onmouseout="MakeOpaque(evt)" 
    onmouseup="buttonClick2()"/>      
   </g>                                   
  );    
}
while (@data = $sth->fetchrow_array()) {
print qq(  
    <g transform="scale(1,-1)"  >
       <circle           
         ID="$data[0]" 
          cx="$data[1]"        
            cy="$data[2]" 
              r="0.17"
            d="$data[6]"
          d2="$data[7]"
        d3="$data[8]"                
      fill="red"
     onmouseup="buttonClick()"/>

</g>    
  ); 
 }

Thank you

Answer №1

function getid(ID){ 
    return document.getElementById(ID);
}

In this case, you are simply searching for an element with the specific id 'ID'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What causes the AJAX JSON script to output an additional 0?

Within my WordPress website, there is an AJAX function that reaches out to a PHP function in order to retrieve a specific value from a transient record stored in the Database. Whenever I trigger this function with jQuery, I successfully receive the result ...

What is the process for setting up the Google Maps API within an Angular application without relying on any directives

Currently, I am attempting to integrate Google Maps into the application that I am developing. Utilizing the Places API for certain functionalities has been successful thus far. However, I have encountered an issue when trying to display a map on a specifi ...

Use the onclick event to submit a form, ensuring that it can only be submitted once

For a form, I am configuring the action and triggering submission using the onclick event of a div: <div class="action_button" onclick="document.forms['test'].action='url/to/action';document.forms['test'].submit()"> < ...

Disregarding 'zIndex' directive within JavaScript function, an image stands apart

There is an issue with the z-index values of rows of images on my webpage. Normally, the z-index values increase as you scroll further down the page. However, I want certain items to have a lower z-index than the rest (except on hover). These items are i ...

Modify the hash URL in the browser address bar while implementing smooth scrolling and include the active class

I have implemented a smooth scroll technique found here: https://css-tricks.com/snippets/jquery/smooth-scrolling/#comment-197181 $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replac ...

JavaScript - Deleting the last element of an array

I have currently integrated a third-party API to visualize data using Highcharts. This external API provides data specific to the current month, such as March, April, May, and so on. graphArrOne contains an array with 251 elements. graphArrTwo contains ...

Tips for sending a parameter within a JavaScript confirm method?

I currently have the following code snippet in my file: <?php foreach($clients as $client): ?> <tr class="tableContent"> <td onclick="location.href='<?php echo site_url('clients/edit/'.$client->id ) ?>&ap ...

What is the best way to incorporate an ASYNC function within a .map function to dynamically populate a table in a REACT application?

I am attempting to use the .map() method in REACT to call an async function and populate table data cells. The async function communicates with the backend of my application using data from the array being looped through. When called correctly, the functi ...

What steps do I need to take to ensure my TypeScript module in npm can be easily used in a

I recently developed a module that captures keypressed input on a document to detect events from a physical barcode reader acting as a keyboard. You can find the source code here: https://github.com/tii-bruno/physical-barcode-reader-observer The npm mod ...

Is there a way to output base variables in Gurobi using Java?

I need to output a 1 if the variable is a base variable, otherwise output a 0. I attempted this code but it's not working as expected: for(GRBVar var : model.getVars()){ double v = var.get(DoubleAttr.RC); if(v == 0) System.ou ...

Do not start Chart.js on the Y-Axis

As of now, this is the current appearance of the code which includes a Chart.js first image preview. I am seeking advice on how to prevent my data chart from starting at the Y axis. The result of the code can be viewed below. https://i.sstatic.net/sdl3K. ...

Error encountered: A syntax error occurred due to an unexpected token ":" found in the file path D:practise odejs odejs-demoviewsindex.html

Attempting to switch the engine view from .ejs to .html resulted in an error. After searching online, I was unable to find a solution for the following problems: Express 500 SyntaxError: Unexpected token : in D:\practise\nodejs\nodejs-demo& ...

When representing audio as sound bars on a canvas, the previous drawing is retained if canvas height is not specified

After obtaining an audioBuffer containing an audio clip, I proceed to create a visualization by drawing a series of sound bars in the shape of a circle: const { audioContext, analyser } = this.getAudioContext(); const source = audioContext.createBufferSou ...

Hello, I'm looking for assistance with this ReactJS code. Is there anyone who can help me

I am not receiving any output and also not encountering any errors, how can I resolve this issue? import React from 'react' function Array() { function myConcat() { const Villain = ["Harley Quinn", "Brainiac", "Deathstroke"]; cons ...

Tips for adding text dynamically to images on a carousel

The carousel I am using is Elastislide which can be found at http://tympanus.net/Development/Elastislide/index.html. Currently, it displays results inside the carousel after a search, but I am struggling to dynamically add text in order to clarify to use ...

trouble with phonegap javascript ajax integration

I'm new to app development and I've been trying to create a mobile app using PhoneGap. I have a remote shared server that contains a MySQL table. My goal is to sign up a user, then send the data via JavaScript and AJAX to a PHP server page that w ...

Developing a Java Spring Boot API endpoint to retrieve a JSON representation of a collection of objects

I am currently working on developing an endpoint that will provide a JSON response containing a list of objects. The structure of the objects is as follows: units: [ { id: #, order: #, name: "" concepts: [ ...

Unable to submit form in Nextjs using react-bootstrap

Instructions To create a registration form, follow these steps: Fill out the form on the register page and then click submit. The react-bootstrap button will trigger the handleSubmit() function using onSubmit={}. Expected vs Actual Outcome I attempted va ...

Analyzing the structure according to the month/week/year

My array consists of count and date values: day = [ { count: 1, date: '2022-07-07' }, { count: 1, date: '2022-08-14' }, { count: 2, date: '2022-07-19' }, { count: 4, date: '2022-07-19' }, { count: 2, date: ...

How to target only the parent div that was clicked using jQuery, excluding any

I have attempted to solve this issue multiple times, trying everything I could find on Google and stack overflow without success. At times I am getting the span element and other times the div - what could be causing this inconsistency? $(".bind-key"). ...