Fabian Hell
Fabian Hell

Reputation: 41

jQuery - Change the text of a id

<div id="responsive" class="modal fade" tabindex="-1" data-backdrop="static" data-keyboard="false">
    <div class="modal-header">
        <h4 id="processorTitle" class="modal-title">Responsive</h4>
    </div>
    <div class="modal-body">
        <div class="row">
            <div class="col-md-12">

            </div>
        </div>
    </div>
</div><!-- /modal -->

I want to change the text of "processorTitle" but it is not working.

jQuery:

$(".changeLanguage").click(function(){
  $("#processorTitle").html('awesome text');
  $("#responsive").modal("show");
  $.get(window.assetsFolder + "files/languages/" + $(this).attr("requestLanguage") + ".lang")
      .done(function(data) {

      })
      .fail(function() {

      });
});

Any ideas?

Upvotes: 0

Views: 53

Answers (4)

George Cummins
George Cummins

Reputation: 28906

Form elements have text properties, but other HTML elements (like the H4 in your example) do not. Instead, they have innerHTML.

In plain JavaScript, you can access the innerHTML:

document.getElementById('processorTitle').innerHTML;

or change it:

document.getElementById('processorTitle').innerHTML = 'your text';

To use jQuery to access the value of innerHTML, use .html():

$('#processorTitle').html();

or change it:

$('#processorTitle').html('your text');

If you continue to have trouble after making this change, be sure that you included the jQuery library into your page to use the jQuery features:

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

Upvotes: 0

baao
baao

Reputation: 73241

JavaScript:

document.getElementById('processorTitle').innerHTML='awesome text';

jquery:

$('#processorTitle').html('awesome text');

Upvotes: 0

Colin Schoen
Colin Schoen

Reputation: 2592

Assuming you are including the jQuery library which isn't included in your code snippet, but inferred based on your first attempt:

$("#processorTitle").html("test");

Upvotes: 0

Schien
Schien

Reputation: 3903

A classic JavaScript line should just do the job:

document.getElementById('processorTitle').innerHTML=' new text ';

Upvotes: 1

Related Questions