When you print document.getElementById('idname').value, it only displays [object HTMLInputElement]

Currently, I have a Tamper-monkey script in place that is designed to scrape specific text from a single page. The script's job is to extract the values of three buttons named pick_0, pick_1, and pick_2 from the page and display them in a designated box further down on the same page. Although the process is slightly more intricate than described, my primary goal right now is to ensure that it can successfully retrieve these values.

The major issue I am facing is that the script has been unable to capture the values accurately. I'm uncertain whether this discrepancy is due to how it's accessing the element or how it's interacting with the page itself.

On every resource I consult for a solution, the addition of ".value" to the code is always recommended to rectify the problem. Despite implementing this fix, I've reached a standstill which has led me to seek help through this inquiry.

A colleague shared the webpage scraping code with me, causing some concern as to its effectiveness. I've also experimented with alternative solutions sourced from Stack Overflow without any success thus far. It's worth mentioning that the TamperMonkey script is configured to execute exclusively on the specific page requiring scraping, although this detail wasn't explicitly mentioned within the script itself.

function getLink(url) {
    return $.ajax(
        {
            type: 'GET',
            async: true,
            url:  url,
        });     
}   

Here are the variables I am working with:

function inputs() {
    var pick_0b = document.getElementById('pick_0').value;
    var pick_1b = document.getElementById('pick_1').value;
    var pick_2b = document.getElementById('pick_2').value;
}

The functionality responsible for displaying the extracted data is functioning correctly when tested with a dummy variable exhibiting similar syntax. However, whenever I try to output the values from the aforementioned buttons, I only receive one of these two results:

undefined

[object HTMLInputElement] 

Seeking assistance in resolving this matter. Edit: Below is the HTML markup for the buttons:

<tr><td class="centered"><input type="submit" name="pick_0" value="longass string of randomized text here"  class="button" id="pick_0" /></td></tr>
<tr><td class="centered"><input type="submit" name="pick_1" value="longass string of secondary random text here"  class="button" id="pick_1" /></td></tr>
<tr><td class="centered"><input type="submit" name="pick_2" value="longass third string of random text here"  class="button" id="pick_2" /></td></tr>

Answer №1

function inputs() {
    var select_0b = document.getElementsByName('select_0');
    var select_1b = document.getElementsByName('select_1');
    var select_2b = document.getElementsByName('select_2');
}


To retrieve:


alert(select_0b[0].value);

Do any of these functions properly?


function inputs() {
    var select_0b = document.getElementsByName('select_0');
    var select_1b = document.getElementsByName('select_1');
    var select_2b = document.getElementsByName('select_2');

    alert(select_0b[0].value);
}


or


var select_0b = ""
var select_1b = ""
var select_2b = ""

function inputs() {
    document.getElementsByName('select_0');
    document.getElementsByName('select_1');
    document.getElementsByName('select_2');
}

alert(select_0b[0].value);

Edit: By this point, you may have addressed this issue... if not...

I revisited your pastebin file... tampermonkey might be as particular as greasemonkey when it comes to the metadata block - ensure the second column aligns perfectly.

Original:

// ==UserScript==
// @name       Result Reader
// @namespace  tarakore.com
// @version    1.0
// @description  Stupid description goes here.
// @match      http://www.(removedurl).com/game/* //Url removed because the site in question wouldn't want attention from this post.
// @copyright  2014+ Tarakore/Katiki
// @grant   GM_xmlhttpRequest
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
// ==/UserScript==

Potential fix:

// ==UserScript==
// @name         Result Reader
// @namespace    tarakore.com
// @version      1.0
// @description  Stupid description goes here.
// @match        http://www.(removedurl).com/game/* 
// @copyright    2014+ Tarakore/Katiki
// @grant        GM_xmlhttpRequest
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
// ==/UserScript==

You might also consider omitting the copyright line for good measure.

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

I aim to retrieve the names of all directories

I am seeking assistance from seniors in creating a dropdown list of root directories using PHP. I have almost completed the task, but I am facing an issue with not being able to retrieve the root directory. For example, I want all directories like home/ab ...

What is the best way to partition JSON data from an API request in React and display it in various sections within the component

There are 2 JSON objects that contain sales period details based on Month to date and year to date. The data includes information such as Units Sold, Gross Revenue, Year to Date Totals, Month to Date Averages, Expenses, Net Revenues, and Per Unit values. I ...

Determining when a function is triggered from the JavaScript console

Is there a way to conceal a function in JavaScript console so that it's inaccessible for calling? Let me give you some context - let's say I have a JavaScript function that adds records to a database using Ajax. The issue is, anyone can call thi ...

Can you explain the distinctions among <Div>, <StyledDiv>, and <Box sx={...}> within the MUI framework?

When exploring the MUI documentation, I came across a table of benchmark cases that can be found here. However, the differences between the various cases are not clear to me. Can someone please explain these variances with real examples for the following: ...

Reorder elements in CSS Grid

I've been working with a css-grid and it's almost there, but I'm stuck on reordering the items in a specific way. Currently, the html layout is set as “1, 2, 3, 4,…10”, but for smaller screens, I want the visual order to be “1, 2, 4 ...

Understanding Vue.js: Effectively communicating updates between parent and child components using scoped slots

My goal is to develop a component with two slots, where the second slot repeats based on the number of items in the first slot. I have successfully implemented this using scoped slots. However, I noticed that when the data in the first slot is updated, the ...

Triggering on multiple clicks - MULTIPLE CLICKS

I'm currently in the process of developing a website and I have a specific requirement where I need an image to change on click to another image, and then to a third image on the second click, and so forth. I've managed to create a JavaScript fu ...

Is the conditional module require within an "If" statement always executed in nuxt.config.js?

Despite the missing file mod.json, the "require" statement is still executed leading to an error. The condition should have prevented it from entering the "if" block: const a = 1; if(a === 2){ const mod = require('./scripts/mod ...

Tips for replacing default arrow icons with 'Previous' and 'Next' buttons in a Material-UI pagination element

I've been struggling to find a solution with my code provided below. Despite multiple attempts, the issue remains unresolved. import React from "react"; import { gridPageCountSelector, gridPageSelector, gridPageSizeSelector, useGridA ...

Unable to successfully import Node, JS, or Electron library into Angular Typescript module despite numerous attempts

I'm still getting the hang of using stack overflow, so please forgive me if my question isn't formulated correctly. I've been doing a lot of research on both stack overflow and Google, but I can't seem to figure out how to import Electr ...

Link JSON filters to specific JSON nodes on the map

I have data representing nodes and links for a force directed graph. The links can have different numbers of connections between them, such as one, two, or three links: {"nodes": [{"id": "Michael Scott", "type": "boss"} ,{"id": "Jim Halpert", "t ...

Are you in need of a JavaScript data validation tool?

Trying to find a library that can validate input in a specific format, such as: { points: array of { x: positive and less than 20, y: positive and less than 15 } } Ideally, it should work on both server and client sides and either return a boolean or th ...

Step by step guide on integrating ReactJS into your current node.js backend application

I am currently working on a basic node.js API Setup: | project-name | public | index.html | ... some static js/css | app.js | package.json app.js var express = require('express'), bodyParser = require('body-parser'), ...

When running the `npm run dev` command, Tailwind does not seem to function

I have been given a task to create forms using tailwindcss, but when I try to run `npm run build`, it doesn't work. Can anyone assist me with this? npm ERR! code ELIFECYCLE npm ERR! errno 9 npm ERR! <a href="/cdn-cgi/l/email-protection" class="__cf ...

Create HTML elements based on the information in a JSON object

My goal is to create span elements for each word in my subtitle text, which is stored in a JSON object. Here is the JSON data I am working with: var sub_info = [ {'start': 3.92, 'end': 6.84, 'words ...

Transform **kerry James O'keeffe-martin** into **Kerry James O'Keeffe-Martin** using TypeScript and Java Script

Is there a way to capitalize names in both TypeScript and JavaScript? For example, changing kerry James O'keeffe-martin to Kerry James O'Keeffe-Martin. ...

What causes the toggle effect in my jQuery onclick function to alternate between on and off when the initialization is repeated multiple times?

I am facing an issue with my website where icons/buttons trigger a menu when clicked. I need to load more data by adding more buttons, so I tried re-initializing the existing buttons using a jQuery onclick function whenever the number of buttons changes. ...

Guide on redirecting to a new domain using a cookie in Express.js

I am working on a web app using Express on Node and I want to add a proxy login feature that allows users to be automatically logged in and redirected to another site after logging into my site. Within my routing function, I have the following code: res ...

Utilizing prototype JS to create a dynamic floating widget or banner

I recently developed a custom script based on the original jQuery code found at this link: However, I encountered issues when attempting to implement it with prototype JS. Specifically, there seems to be accuracy problems with calculating the upper and lo ...

Clicking the download button will trigger the file to be downloaded every time

Within the documents table, I am iterating through metadata to extract a filename and docid which are then passed to my DocumentDownloadButton component: const DocumentsTableBody = ({ documentMetadata, tableProps }) => { const { Row, Data } = Table ...