Davor Budimir
Davor Budimir

Reputation: 416

How to override html element from JavaScript file

I have this code inside my js file. Can you tell me how can I change text inside div with class "message".

$.post(url, form_serialized, function(data) {
      form.html('<div class="message">Thank you for contacting us!</div>');
});

So I want to change "Thank you for contacting us" that's inside .js file, to override it inline in html to "Thank you for sending us a message".

Upvotes: 0

Views: 1033

Answers (2)

Jithil P Ponnan
Jithil P Ponnan

Reputation: 1247

You have to change your codes little in your js file

  var textToBePassed ="'<div class="message">Thank you for contacting us!</div>'";

    $.post(url, form_serialized, function(data) {
          form.html(textToBePassed );
    });

now just add this code in HTML file as

 textToBePassed ="Thank you for sending us a message"

Before calling it.

Upvotes: 0

Jonno_FTW
Jonno_FTW

Reputation: 8809

In your html, you need

<script type="text/javascript">
$(document).ready(function() {
    $.post(url, form_serialized, function(data) {
      form.html('<div class="message">Thank you for contacting us!</div>');
    });
});
</script>

Although I suspect that you want this to be tied to some button press. So you may want:

<script type="text/javascript">
$("#form_id").submit(function(ev) {
    ev.preventDefault();
    // .... setup form_serialized variable with form data
    $.post(url, form_serialized, function(data) {
      form.html('<div class="message">Thank you for contacting us!</div>');
    });
});
</script>

Where the form tag looks like

<form id="form_id">

Upvotes: 1

Related Questions