Entering data from an array into a textbox using JavaScript

As a newcomer to JavaScript, I've been encountering difficulties when it comes to transferring information extracted from a custom-built array into a text box. My current approach involves using Dashcode and tweaking elements within the main.js file as needed. While I have successfully created an array and can access values stored within it, I'm struggling to populate the text box with this data.

The specific line causing issues is:

document.getElementById("text").setAttribute("text", textLocation);

In this snippet, text refers to the ID of the target text box, and textLocation represents the content I aim to insert into it.

Your assistance on this matter would be greatly appreciated.

Below you will find the remainder of the code:


var n = null;
var textLocation;
var myArray = new Array("info one","info 2","info 3","info 4","info 5","info 6","info 7","info 8","info 9");

function toPreviousImage(event) {

    var list = document.getElementById("grid").object; 
    var selectedObjects = list.selectedObjects();
    var name = selectedObjects[0].valueForKey("name");
    var textinfo = selectedObjects[0].valueForKey("info"); 

    name= name - 1;

    if(!n || n == undefined){n=name} else {n--}

    textLocation = myArray[n];

    document.getElementById("text").setAttribute("text", textLocation);

Answer №1

In my opinion, the attribute that needs to be set is 'value' instead of 'text'.

document.getElementById("text").setAttribute("value", textLocation);

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

Creating a stylish CSS button with split colors that run horizontally

Could you please provide some guidance on creating a button design similar to this one? I've made progress with the code shown below, but still need to make adjustments like changing the font. <!DOCTYPE html> <html> <head> <sty ...

Is it crucial to pay attention to the warning about passing incompatible pointer types? How to properly pass a multi-dimensional array to a function

Looking to create a function that can handle multidimensional arrays using pointers. Take the following example: #include <stdio.h> void print_matrix(int channel, int row, int column, int *matrix); int main(void) { int test[4][5][6]; int co ...

Matching Strings in JavaScript and Jquery

Can you assist me in comparing two strings? I need a solution that will return true if str1 matches 80% of str2, and false otherwise. Thank you for your help. ...

Extract data from a multidimensional $_POST array

I am having trouble retrieving values from the $_POST array below: [name] => Joy [adq12] => Array ( ['qst'] => Yes ['ans'] => Occasionally ) [lang] => A ...

Exploring the Relationship Between CSS Attribute Values and Strings in Eclipse

Looking to conduct a test to determine whether the value of a CSS attribute matches a specific string. The comparison functions correctly in JSFiddle, providing the expected result. However, Eclipse presents a different outcome and appears to interpret the ...

Does IE 9 support endless local storage capacity?

I recently updated my HTML5 persistent storage (localStorage) capacity testing to address a memory exception issue in the previous version. I even created a jsFiddle showcasing it: http://jsfiddle.net/rxTkZ/4/ The testing code involves a loop: var val ...

Acquiring route parameters in modal elements within Angular

Having trouble accessing route params data within modal components. Current URL: http://localhost:4200/portal/profiles/edit/5303 Route Configuration: { path: ':accountId', component: ProfileOverviewComponent, data: { title: ' ...

Restrict the frequency with which a button may be clicked - coding with C#, ASP.NET MVC5, and JavaScript

Is there a way to prevent creating multiple entries when clicking the Create button on my page for Income Types? Upon inspecting the code, I found that it utilizes an ajax method to retrieve information and submit the form to the database. Here is some of ...

The stunning fade effect of Magnific Popup enhances the visual appeal of the inline gallery

I'm currently using magnific popup for an inline gallery, but I'm not seeing any effects. I would like to have a fade effect when opening/closing the popup. Can someone assist me with achieving this? https://jsfiddle.net/gbp31r8n/3/ [Check o ...

Have I potentially compromised my project by executing the command `npm audit fix --force`?

While working on a React project using Vite, everything was going smoothly. I decided to incorporate some charts and came across the recharts package, which I found quite appealing. So, I added it to my project using the command npm i recharts. After runn ...

Leveraging JavaScript to compare two arrays and generate a novel array of objects that excludes any items with matching id values

I have a task at hand where I need to create a new array in JavaScript that does not contain objects matching by their id field, using two arrays of objects. The first array consists of: [{id: 1, name: "Jo"}, {id: 2, name: "Pat"}, {id: 3, name: "Mike"}] ...

Sending a PHP array through AjaxPassing a PHP array

Here is the array in question: $users = array(); // Loop through results and assign to $users array foreach( $select as $email => $ip ) { $users[$email] = $ip; } I now need to send this array to an AJAX request and display it in another script ...

Shadow effect on the dropdown body with accordion style

I've been developing an accordion control for my website that is nested inside a card. I have applied a box shadow to the header of the accordion which looks fine, but when it expands, the body pops up with a shadow as well. I'm trying to figure ...

Tips for sending data to CSS in Angular

I have an Angular application where I need to calculate the width of an element and store it in a variable called finalposition. Then, I want to move this element to the left by (finalposition)px when hovering over it. How can I achieve this styling effect ...

How can I display all values in Meteor Blaze by iterating through a mongo cursor?

Can someone please guide me in the right direction? I have an application that uploads a CSV file to MongoDB and then displays it within the Meteor framework. I am facing an issue where I am subscribing to the data in the Template.onCreated function, but I ...

How can I resolve the Error: Element type is invalid?

https://i.sstatic.net/9PehR.jpgI am encountering the error message displayed above, and I am uncertain about the cause of this issue. In the code snippet below, I am fetching data from a file named Data.js located in my root folder. When I run the app, I r ...

The method IFrame .appendto is not compatible with Safari browsers

There is a web page with a submit event that utilizes jQuery to store form data on the web server as a third party. The code functions by adding an IFrame to the html page's body, transmitting data through the query string via the IFrame 'src&apo ...

Leverage Java variables within JavaScript

Currently encountering an issue with integrating a java variable (obtained from a database record) into JavaScript for the datePicker UI. The goal is to assign the java variable to the javascript var "unavailable" so that already booked dates become unclic ...

implementing advanced filtering and pagination using ajax requests in jQuery

I have successfully implemented filtration and pagination separately in my code. However, when I try to combine the two functionalities into one script, the pagination feature is not working as expected. Below is the snippet of code that I have been workin ...

Issue with OBJLoader showing a blank screen

Does anyone have experience with rendering OBJ files using three.js? I'm having trouble getting them to display on the screen and would appreciate any help or advice. The strange thing is that a simple cube renders perfectly in my project. For those ...