-3

I have got a task to create a game. For that I need some help... How can I change the value of a div by clicking on it?

<div class="block1">    
         <div id="two">Two</div>
</div>

js code :

 $( ".block1" ).click(function() {
          $( "#one" ).css('display','block');
          $( "#two" ).css('display','none');
          $( "#three" ).css('display','none');
          $( "#four" ).css('display','none');   
           });

DEMO

Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84
  • 2
    The code here doesn't match the code in your fiddle. What exactly are you looking to do? – j08691 Jul 01 '13 at 16:23
  • 2
    possible duplicate of [Using jQuery, how do I change the elements html value? (div)](http://stackoverflow.com/questions/537554/using-jquery-how-do-i-change-the-elements-html-value-div) or rather [jquery set value of div](http://stackoverflow.com/questions/1570905/jquery-set-value-of-div) – Paul Jul 01 '13 at 16:24
  • Change the 'value' *how*? – David Thomas Jul 01 '13 at 16:26
  • 1
    Your fiddle has duplicate id's for elements and is thus invalid HTML and none of the answers accessing an element by id will work properly. – Mark Schultheiss Jul 01 '13 at 16:29
  • 1
    Hasn't this been already answered thousand times?... – A. Wolff Jul 01 '13 at 16:30

4 Answers4

2

Use .text to change the text

$('#one').text('New value')

You can use the this context inside the click event to change the text of currently clicked div.

 $(".block1").click(function () {
      $(this).text("New value")
  });

Check Fiddle

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
2
$( ".block1" ).click(function() {
$(this).html("Div Content")
}
Vishnu Sureshkumar
  • 2,246
  • 6
  • 35
  • 52
0

Approach - 1

$("#one")[0].textContent = "test";

Approach - 2

$("#one").prop('textContent', "test");
Imad Alazani
  • 6,688
  • 7
  • 36
  • 58
0

Assuming you are using jQuery

You can use .text() to change or get the text inside the div

$("#one").text('Some new text');

You can use .html() to change or get the html inside the div

$("#one").html('<p>Some new html</p>');

For form elements such as you can use .val()

$("#one").val('1');
Pattle
  • 5,983
  • 8
  • 33
  • 56