Change textarea background color when button is clicked

Asked

Viewed 741 times

2

I developed an application in Javascript, Jquery and HTML.

I would like to make sure that when the user clicks the button btnalterar, to textarea created change your background color.

Does anyone have an idea how to do that?

<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> <!-- add Jquery CDN -->
        <link href src="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> <!-- add Bootstrap CDN -->
        <link href="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>

  • You have styles implemented right after the opening of the tag <body> that should be on <head> right after the bootstrap.css instead.

  • Looking at it, there are more errors than that. You had a poorly closed CSS link, it was <link href src="..."></script>, when you should be <link href="..."/>.

1 answer

1

I know very little Javascript, but you can program for when the btnalterar is clicked, it change the style of 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> <!-- add Jquery CDN -->
        <link href src="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> <!-- add Bootstrap CDN -->
        <link href="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.