Change textarea's background color when button is clicked

2

Developed an application in Javascript, Jquery and HTML.

I would like to make when the user clicks the btnalterar button, the textarea created changes their background color.

Does anyone have an idea how to do this?

<html>
    <meta charset="UTF-8"> <!-- add special characters -->
    <head>
        <title>Página</title> <!-- Add page title -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><!--addJqueryCDN--><linkhrefsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" media="screen"/> <!-- add bootstrap css CDN -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><!--addBootstrapCDN--><linkhref="design.css" rel="stylesheet" media="screen"> <!-- Add css in html -->
    </head>

    <body class="corpo">
        <h1><center>Página Monstra</center></h1>
        <hr/>
        <script type="text/javascript"> // all javascript/jquery code start here

            function add_field() 
            {

                // ------------------------------------------------------
                var form = document.getElementsByTagName('form')[0],
                    input = document.createElement('textarea');
                input.setAttribute('type', 'textarea');
                input.setAttribute('name', 'item');
                form.appendChild(input);

                input = document.createElement('button');
                input.setAttribute('type', 'button');
                input.setAttribute('name', 'item');
                input.setAttribute('class', 'btnalterar');
                form.appendChild(input);
            };

            var count = 0;
            function contador() 
            {
                count++;
                console.log(count); // show count in console

                if(count >= 10){
                    alert("VOCÊ NÃO PODE ADICIONAR MAIS CAIXAS"); // alert dialog
                    $( ".button" ).prop( "disabled", true ); // disable button

                }
            };
        </script>

        <form name="input" method="get">
            <div class="ui-input-text">      
                <div data-role="navbar">
                    <button type="button" class="button" onclick="add_field(); contador();">ADICIONAR CAIXAS DE TEXTO</button><br><br> <!-- Create add button -->
                </div>
            </div>
        </form>

        <!-- SALVAR -->

        <form name="input" method="get">
            <div class="ui-input-text">      
                <div data-role="navbar">
                    <button type="button" class="btnsave">SALVAR</button><br><br> <!-- Create save button -->
                </div>
            </div>
        </form>

    </body>
</html>
    
asked by anonymous 24.09.2017 / 13:45

1 answer

1

I know very little of Javascript, but you can program for when btnalterar is clicked, it changes style from textarea :

<html>
    <meta charset="UTF-8"> <!-- add special characters -->
    <head>
        <title>Página</title> <!-- Add page title -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><!--addJqueryCDN--><linkhrefsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" media="screen"/> <!-- add bootstrap css CDN -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><!--addBootstrapCDN--><linkhref="design.css" rel="stylesheet" media="screen"> <!-- Add css in html -->
    </head>

    <body class="corpo">
        <h1><center>Página Monstra</center></h1>
        <hr/>
        <script type="text/javascript"> // all javascript/jquery code start here
            function add_field() 
            {
                // ------------------------------------------------------
                var form = document.getElementsByTagName('form')[0],
                    input = document.createElement('textarea');
                input.setAttribute('id',count.toString());
                input.setAttribute('type', 'textarea');
                input.setAttribute('name', 'item');
                form.appendChild(input);

                input = document.createElement('button');
                input.setAttribute('onclick',"document.getElementById(" + count + ").style = 'background-color:blue;';");
                input.setAttribute('type', 'button');
                input.setAttribute('name', 'item');
                input.setAttribute('class', 'btnalterar');
                form.appendChild(input);
            };

            var count = 0;

            function contador() 
            {
                count++;
                console.log(count); // show count in console

                if(count >= 10){
                    alert("VOCÊ NÃO PODE ADICIONAR MAIS CAIXAS"); // alert dialog
                    $( ".button" ).prop( "disabled", true ); // disable button
                }
            };
        </script>

        <form name="input" method="get">
            <div class="ui-input-text">      
                <div data-role="navbar">
                    <button type="button" class="button" onclick="add_field(); contador();">ADICIONAR CAIXAS DE TEXTO</button><br><br> <!-- Create add button -->
                </div>
            </div>
        </form>

        <!-- SALVAR -->

        <form name="input" method="get">
            <div class="ui-input-text">      
                <div data-role="navbar">
                    <button type="button" class="btnsave">SALVAR</button><br><br> <!-- Create save button -->
                </div>
            </div>
        </form>

    </body>
</html>
    
24.09.2017 / 14:29