简体   繁体   中英

Alert with a textarea content (HTML to JavaScript)

I'm new at coding with any language and now im working (trying to) with HTML, PHP and JavaScript. My difficulty is to show through an alert in a JavaScript file the information that is in a textarea from an HTML file. The HTML code is this:

    <head>
        <title> My Form</title>
        <script type= "text/javascript" src ="./JavaScript/validaLinha.js"></script>
    </head>
    <body>
        <form name="Linhas" method="POST" action="Linhas-p.php">
             <textarea name="descricaoLinha" onFocus="resetField('descricaoLinha')"> TEXT-TEXT-TEXT</textarea>
        </form>
    </body>

As you can see at the HTML code, I try to send the name of the textarea to a JavaScript function called "resetField". Let's see what the "resetField" does:

function resetField(field){
    d = document.Linhas;

    alert("It's entering the function."); //ANSWER = It's entering the function.
    alert(field);                         //ANSWER = descricaoLinha
    alert(d.field.value);                 //ANSWER = Nothing.
    alert(d.getElementById(field).value); //ANSWER = Nothing.
}

I can't get the information set at the value of my textarea! The event is calling the function (First alert shows up), the string is being received by the function (Second alert shows the name of the text area) but the other two that are the ones that i need doesn't show up. I already tried to change the order of the alerts because of that JavaScript stuff that doesn't continues to read the code if an error appears.

Just to emphasize, i want the content IN the textarea. (The name of it is being received properly)

Thanks for reading! Sorry about my english. ^^

Add an ID to your textaea

<textarea id="descricaoLinha" name="descricaoLinha" onFocus="resetField('descricaoLinha')"> TEXT-TEXT-TEXT</textarea>

And then, send the ID instead of name

alert(d.getElementById(field).value);

This code can't find the element descricaoLinha because its searching for an element that has an ID = " descricaoLinha "

Your 'field' is just a string, not a javascript object and of course it does not have the name attribute;

And it is similar to the d = document.linha;

You have have to get the DOM element by its id attribute:

d = document.getElementById("Linha");


Edit: You also need add the id attribute to your field

<textarea id="descricaoLinha" name="descricaoLinha" onFocus="resetField('descricaoLinha')"> ...

One solution is: you change your JavaScript function to

function resetField(fieldId) {
    var field = document.getElementById(fieldId);
    alert(field);           // object
    alert(field.value);     // text field value    
    // Do whatever you want with the field object here 
}

You should turn on the console and using console.log('output') instead of using alert() for debugging; You can turn on Firefox console by Ctrl + Shift + K

You should pass in this in your onfocus handler:

onFocus="resetField(this);"

Access it like:

alert("It's entering the function."); 
alert(field.name);                       
alert(field.value);

Here's a demo: http://jsfiddle.net/RbUZr/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM