3

Inside my html file I call a javascript function that takes two parameters, where the second parameter is the name of a file that should be saved.

<a id="record_button" onclick="Recorder.record('audio', 'test.wav');" href="javascript:void(0);" title="Record"><img src="images/record.png" width="24" height="24" alt="Record"/></a>

I would like to make a dynamic variable that takes the value from a textfield and forward that as the second parameter (instead of test.wav), so the user can determine the name of the file.

<label for="filename">Filename</label>
<input name="filename" type="text">

Thanks.

3 Answers 3

12

This is easier if you give your user input an id attribute:

<input name="filename" id="filename" type="text">

Now you can access the value in Javascript with:

document.getElementById('filename').value

Note that, if you aren't controlling the Recorder.record() method, you'll need to validate the user input first (at a minimum, to verify that they've entered something). I'd recommend moving this to a separate function, e.g.:

function recordToFilename() {
    var input = document.getElementById('filename'),
        fileName = input.value;
    if (fileName) {
        Recorder.record('audio', fileName);
    } else {
        alert('Please enter a filename!');
        input.focus();
    }
}

Then just use that function:

<a id="record_button" onclick="recordToFilename();" href="javascript:void(0);" title="Record"><img src="images/record.png" width="24" height="24" alt="Record"/></a>

Working example: http://jsfiddle.net/nrabinowitz/GFpRy/

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

4 Comments

Good one. Just add a fileName.focus() below to alert :)
@Márcio - good idea - added above. You could also just make the alert a prompt and maybe ditch the input altogether...
Thanks for the reply. I have created a <script>record to filename function</script> inside my HTML file, and to exactly as you have written. However nothing occurs when I press the record button. As soon as I change the onclick to Recorder.record('audio', 'test.wav'); it works, but when changing to recordToFilename(); it doesn't
Do you get any Javascript errors? How are you defining the function? See the working example I included above.
2
Recorder.record('audio', document.yourformname.filename.value)

Make sure to have a name="?" attribute for your form for this to work and replace yourformname with what you put as the form's name. filename refers to the input's name attribute as well.

Comments

2
<html>
<head>
  <script>
     function addData (fn, ln, em) {

       console.log(fn);
       console.log(ln);
       console.log(em);

     }
 </script>
</head>
<body>
    <input Type="text" name="FirstName" id="fn">
    <input Type="text" name="LastName" id="ln">
    <input Type="email" name="Email" id="em">
    <button onClick="addData(fn.value,ln.value,em.value)">click</button> 
</body>
</html>

3 Comments

Please add explanations to your answer.
when javascript code is fix but html field id is change then we can use this code.
I can work it out, but I doubt the OP can if they had little experience (although after 6 years of JavaScript he probably should be able to).

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.