Jquery - how to change div text after submit form

I have a form for entering directions and displaying a google map

<form action="#" onsubmit="setDirections(this.from.value, 'address', 'en_US'); return false">

Outside of the form, I have the following:

Formatted Directions<div id="printdirec"><a href="http://maps.google.com/">Print Directions</a></div>

I want to change the value inside the div tag 'printdirec' to say something else

I tried this (which I thought would work), but it is not:

    <script type='text/javascript'>
        $(document).ready(function() {

        $(":submit").click(function(e) {
                $('div.printdirec').val('tttt')
            });

        });
</script>

Any suggestions on what I'm doing wrong?

+3
source share
4 answers

The # symbol identifies the item identifier. Use .text () to set the inner text.

$('div#printdirec').text('tttt');
+5
source

It would be more reliable to handle an event submitthat produces the following:

$("form#id-of-your-form").submit(function(e) {
    $('#printdirec').html('tttt');
});

. Selectors . , .

+3

:

$('div.printdirec').val('tttt')

searches for any divs with the class printdirec . You will want to access it using id . In addition, you will need to set the div text, not the value. Try the following:

$('#printdirec').text('tttt');
+2
source
$("div#printdirec").html("tttt");

You really have to bring all your sending logic to the same place. No need to have it in the markup.

+1
source

Source: https://habr.com/ru/post/1715851/


All Articles