KDX2
KDX2

Reputation: 1023

How to get a string input and store it into a var

I know it's simple and there's probably something wrong with my syntax but I just don't understated it. I'm talking about the following:

//declaration of a string
var str_input = "";

//this is supposed to get the new inputs and to store them in str_input
$(document).ready(function(){

   str_input = $('input[name=po]').val();

});

//this is on html side, this should make an input field where the user to type in the needed
<input type"text" name = 'po' id="po" value="asd">

That's it, can you help me to sort it out? The problem so far is that str_input is undefined regardless of what is written in the input, though, it saves its initial value.

Upvotes: 1

Views: 246

Answers (3)

Bak
Bak

Reputation: 262

Ok, Now I understood, you can do 2 things, first, you can create a button than when the user clicks it calls the function to store the value in the variable like this:

var str_input = "";

//this is supposed to get the new inputs and to store them in str_input
$(document).ready(function(){
$("#MyButton").click(function(){
str_input = $('input[name=po]').val();
})


});

//this is on html side, this should make an input field where the user to type in the needed
<input type"text" name = 'po' id="po" value="asd">
<input type="button" id="MyButton" value="Click Here" />

Or the blur function when the user lose focus of the input text like this:

   var str_input = "";

    //this is supposed to get the new inputs and to store them in str_input
    $(document).ready(function(){
    $('input[name=po]').blur(function(){
    str_input = $('input[name=po]').val();
    })


    });

    //this is on html side, this should make an input field where the user to type in the needed
    <input type"text" name = 'po' id="po" value="asd">

Upvotes: 1

KDX2
KDX2

Reputation: 1023

Ok, so here is the solution, though it's a little bit in "from Alf to Alf" style. So, yes, the code I've posted in the main post uses correctly the 'by default' value of the input but the problem comes from that nothing is checking for further changes in the input text field. I'm using $(document).ready... which as far as I know runs during the web project is opened and of course enables the use of some jquery methods within it. There is an interesting function called .change() which actually put the whole thing up for me. Take a glance at the following:

 $(document).ready(function(){
 $('input[type="text"]').change(function(){
            str_input = $('input[name=polynom]').val();;
        });
   str_input = $('input[name=polynom]').val();

});

The second, third and fourth line make the magic actually, where the code in the .change() method updates str_input on each time a change have appeared in the input box field, whereas the str_input = $('input[name=polynom]').val(); outside of change() takes into account the initial value of the input. That's it... I knew I was forgetting something and yeah...

P.S. Another approach is to use a function and a button (a.k.a. an event-triggered function) which to 'update' the str_input on event. For this way check the post bellow this one.

Upvotes: 0

taxicala
taxicala

Reputation: 21759

Your html tag is invalid:

<input type"text" name = 'po' id="po" value="asd">

Should be:

<input type="text" name = 'po' id="po" value="asd">
        // ^ Missing this character (=)

Upvotes: 2

Related Questions