0

I'm looking for way to load value from textbox into variable.

I've tried:

<p>
<textarea id="Text" name="Text" rows="6" cols="36"></textarea>
</p>
<p>
<input type="button" name="Button" value="Do it" onclick="javascript:job()" />
</p>
    function job() {

      var data = {};

      data = document.getElementById("Text").value;

      // other part of code
    }

This way doesnt work. If I put my data in {} script works perfectly

Working example:

var data = {
    "Something1": "Otherthing1",
    "Something2": "Otherthing2",
    "Something3": "Otherthing3",
};

But the data is each time different, thats why I need to load it from user input

Edit: Well, working script from console returns data as

Object {Something1: "Otherthing1", Something2: "Otherthing2"} 

Nothing else passes.

0

6 Answers 6

1
  1. remove the javascript: label. It is not needed

  2. In case you want to PASTE JSON into the textfield, you need to use JSON.parse:

function job() {
  var data = JSON.parse(document.getElementById("Text").value);
  console.log(data);
}
<p>
  <textarea id="Text" name="Text" rows="6" cols="36">{"1": "49.9138756N, 16.6112022E", "2": "50.0225236N, 15.7659417E"}</textarea>
</p>
<p>
  <input type="button" name="Button" value="Do it" onclick="job()" />
</p>


First attempt to answer this with limited information:

Your code seems to work here: https://jsfiddle.net/mplungjan/bodmshj4/

function job() {
  var data = {};
  // example 1
  data["text"] = document.getElementById("Text").value;
  console.log("Example 1",data);
  // example 2
  data = document.getElementById("Text").value;
  console.log("Example 2",data);
  // example 3
  data = { 
   "text":document.getElementById("Text").value,
   "Something2": "Otherthing2",
   "Something3": "Otherthing3"
  }  
  console.log("Example 3",data);
}
<p>
  <textarea id="Text" name="Text" rows="6" cols="36"></textarea>
</p>
<p>
  <input type="button" name="Button" value="Do it" onclick="job()" />
</p>

but likely not how you expect.

var data = {} 

assigns an empty object to data.

data = document.getElementById("Text").value; 

overwrites the object with a string.

Perhaps one of these is what you want

var date = {} using 
data["text"] = document.getElementById("Text").value; 

or

data["text"] = document.getElementById("Text").value; 

would set a data.text value -

var data = { 
   "text":document.getElementById("Text").value
}  

will also work for you.

Sign up to request clarification or add additional context in comments.

6 Comments

Well, working script from console returns data as Object {Something1: "Otherthing1", Something2: "Otherthing2"} Nothing else passes. Tried yours code and it returns Object {text: ""Something1": "Otherthing1",↵ "Something2": "Otherthing2","}
Can you show me how you "tried my code" ? I am sure not like this: var data = { "text":document.getElementById("Text").value, "Something2": "Otherthing2", "Something3": "Otherthing3" } - see updated example
Look, example here, this one is working correct - jsfiddle.net/72kgwL4u loadining it from textbox is different
So does this: jsfiddle.net/mplungjan/1qLp8yx7/1 - I removed the external script - it did not belong there, it is only used to display stuff here. I now see Object {1: "49.9138756N, 16.6112022E", 2: "50.0225236N, 15.7659417E", 3: "asdaqd"} if I type asdaqd into the field and click the button
thumbs up @mplungjan
|
1

You can add your object key/value pair by object[key] = value or object.key = value

   <script type="text/javascript">
    var count = 0;
    var data = {};
    function job() {
          data["Something"+(++count)] =document.getElementById("Text").value;      
          console.log(data);
        }        
    </script>

Comments

1

You need to use data.text instead of data

function job() 
{
      var data = '';
      data = document.getElementById("Text").value;
	  console.log(JSON.parse(data));
      // other part of code
}
<p>
<textarea id="Text" name="Text" rows="6" cols="36"></textarea>
</p>
<p>
<input type="button" name="Button" value="Do it" onclick="javascript:job()" />
</p>

15 Comments

@mplungjan sorry I didn't checked comments .. should I remove my answer ??
It's ok :) Nice console btw - I will borrow it :)
@JAdam mark/accept the best suitable answer that helped you to find solution of your problem .... Thank You.
now what actually you want ?? please explain what output do you want??
Exactly this output: jsfiddle.net/72kgwL4u But to textbox it comes like "1": "49.9138756N, 16.6112022E", "2": "50.0225236N, 15.7659417E"
|
0

If you want to use exactly an object you can mod your js function with this one:

var data = {}, index = 0;    
function job() {
   data['Something'+index] = document.getElementById("Text").value;
   index++;
}

Comments

0

Is this ok for you? https://jsfiddle.net/pLrjvk0x/

Even if you do the variable data an Object, value put it into a single string. I splitted it so you can have an Array. I understood you want the data into an Object instead a String and I could only imagine this way to to it (not an object but an Array). If this was not your problem tell me.

var splitted = data.split("\n");

1 Comment

Nope, it returns a lot of unwanted characters
-2

this may help you
var data = document.getElementById("Text").val();

1 Comment

.val() is jQuery. .value is plain JS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.