Can i make a variable equal to html text input?


#1

Can i make a variable equal to html text input?


#2

var myVar;
function getVal() {
    return myVar = document.getElementById("myElt").value;
}

Then call the function. The element that has the value you want to select (assuming textarea) will need the ID of 'myElt', unless you change this in the code above.

I recommend calling the function with an onClick in a button near the element.


#3

thanx
can call it with the click function using jquery


#4

You could, but then you can't do it multiple times.


#5

i tried it like this but nothing works
var ar;
function getVal() {
return ar = document.getElementById("myElt").value;

}
$(document).ready(function () { "use strict";
$('a').click(function() {$('p').append(ar);}) });


#6

You never called getVal()...?


#7

can you show me where to call it ??


#8

Before calling the click function, because you can't append a variable with no value.


#9

Think u very much you helped me a lot


#10

Does it work? :slight_smile:


#11

not yet i dont know what is wrong


#12

Post all of your HTML and JS/jQuery code and I'll try and help :slight_smile:


#13

Html:

<p>

</p>
<a  >slah</a>
<input type="text" id="myElt"  >

javascript & jquery :

var ar;
function getVal() {
  return ar = document.getElementById("myElt").value;
}

 
$(document).ready(function () { 
    "use strict";
    $('a').click( {$('p').append(ar);
}
)}; 
});

#14

Try:

function getVal() {
    ar = document.getElementById("myElt").value; //assigning here
    return ar; //returning here
}

$(document).ready(function () { 
    "use strict";
    $('a').click(function(){
        $('p').append(ar);
    });
});

#15

still dosnt work. :sweat:


#16

Was that your full HTML?


#17

yes it was my full html


#18

If you haven't got a <script> tag, your JS won't be linked, and it won't work. Create the full HTML skeleton before trying to complete this further.


#19

i'm working on jsfiddle


#20

Could you send me a link to the fiddle?