0

Can anyone give me a "trick" how to pass input value to "a" element onclick function.

Let say i have input field with specific id:

<input id="yourname" type="text" value="Testing Value" class="form-control" type="text" placeholder="Your name here..." > 

and function "exportText" which actualy export text (html) from specific div id (biography):

<a   href="#" onclick="exportText('biography', 'Name of player');">

                Save your biography
            </a>

When you click a href link it will save biography in txt document with name: "Name of player.txt"

How can i force the function to actually grab the input id "yourname" value and save the .txt document with that name, something like "yourname+.txt"

I really tried to search a solution over the net, but my knowledge is too low to found an answer on myself.

Thank you so much!

4
  • You have to create one JavaScript function in that you can get the value of input yourname. After that, you can proceed for creating the file. Commented Apr 21, 2020 at 11:41
  • Maybe i didnt say, i already have working function to export file its called "exportText". The function itself has variable/parameter // Specify file name filename = filename?filename+'.txt':'yourname.txt'; Does this piece of code help? Commented Apr 21, 2020 at 11:46
  • Still not clear what you want to say. It would be nice if you share the whole code Commented Apr 21, 2020 at 11:59
  • Can i share you a fiddle link to look at it? Commented Apr 21, 2020 at 12:19

3 Answers 3

2

In your exportText function, if it's a local function you can directly use

document.getElementById('yourname').value;

instead of 'Name of player' parameter Now, if it's a global function, you can play with a specific class (Ex:ClsUserName) and in your exportText use

document.getelementsbyclassname('ClsUserName')
Sign up to request clarification or add additional context in comments.

Comments

1

You can use event:

input id="yourname" value="Testing Value" class="form-control" type="text" placeholder="Your name here..." /> 
<a href="#" id="exportText">Save your biography</a>

and js:

document.getElementById('exportText').addEventListener('click', function() {
    console.log(document.getElementById('yourname').value)
})

Comments

0

No need to use the tag.

Due to the button being inside the form, it will submit the form to the server and reload the page. As the form is submitted, and page load happened you will not be able to see the text assigned to the div.

1 Comment

Not sure which question you are answering, but it does not seem to be this one.

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.