0

I'm trying to code a simple form that will get some text and then display in a dialog box when i click on the submit button.

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <title>Página Teste</title>
</head>
<body>
<header>
    <div>
        <h1>Questão 4</h1>
    </div>
</header>

<div>
    <label for="texto">Texto: </label>
    <textarea name="texto" id="texto" rows="5" cols="10"></textarea>

    <button type="submit" id="submit">Enviar</button>
</div>  
<script>

    var teste = $("texto").val;

    $("submit").on("click", function(){
        $("teste").dialog("open");
    });

</script>
</body>
</html>

When i click on the button nothing happens, and i've tried some ways of doing this script and none seem to work.

4
  • 1
    missing # should be....$("#teste").dialog("open"); and $("#texto").val() Commented Jan 26, 2019 at 13:18
  • Thanks, that was really my mistake, but it's still not working here. I open the .html page, type something, click the button and nothing happens. Commented Jan 26, 2019 at 13:23
  • Missing # ....$("#submit").on("click", function(){ Commented Jan 26, 2019 at 13:26
  • 1) From what I see above, there is no element with an id teste which could act as a dialog. 2) .dialog() is a jQuery-UI method and the library is not loaded. 3) Open the console... You certainly have meaningful errors there. Commented Jan 26, 2019 at 13:33

4 Answers 4

2

You're missing some # and .val is a function so use .val().

I'm not sure I understand what you want to do with the dialog box, but should try something like this:

$("#submit").on("click", function(){
    var teste = $('#texto').val();

    alert(teste);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$(document).ready(function(){
  var teste = $("texto").val;
    $("#submit").on("click", function(){
        console.log("something happens");
    });})

Comments

0

You can do using document ready. Also if you want you can have separate javascript file and do much more operations

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <title>Página Teste</title>
</head>
<body>
<header>
    <div>
        <h1>Questão 4</h1>
    </div>
</header>

<div>
    <label for="texto">Texto: </label>
    <textarea name="texto" id="texto" rows="5" cols="10"></textarea>

    <button type="submit" id="submit">Enviar</button>
</div>  
<script>

 $(document).ready(function(){
   var teste = $('#texto').val();
    $("#submit").on("click", function(){
        alert("teste")
    });})

</script>
</body>
</html>

1 Comment

The document ready wrapper is a nice advise... But in no way is the issue here since the script is below the HTML markup. -- A separate .js file does not allow much more operations at all, it only allow a script to be used in multiple pages.
0

When you call $("teste").dialog("open"); or $("texto").val() jQuery will look for an element <teste></teste> in the dom. Since the element doesn't exist in your example, it won't do anything.

Perhaps you're missing a . or # in the selector you're passing to $(selector)?

Comments

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.