Can i make a variable equal to html text input?

Can i make a variable equal to html text input?

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.

2 Likes

thanx
can call it with the click function using jquery

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

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);}) });

You never called getVal()…?

can you show me where to call it ??

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

Think u very much you helped me a lot

Does it work? :slight_smile:

not yet i dont know what is wrong

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

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);
}
)}; 
});

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);
    });
});

still dosnt work. :sweat:

Was that your full HTML?

yes it was my full html

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.

i’m working on jsfiddle

Could you send me a link to the fiddle?