jim dif
jim dif

Reputation: 641

Getting the contents inside an html tag

I have some simple code below:

<script>
$(function() {
  $("h1").click(function(){
    console.log((this));
    $("p").toggle();
  });
});
</script>
</head>

<body>
<h1>test</h1>
<p>If you click test, I will toggle.</p>

</body>

If I click on test I can toggle the visibility of the p tag. At the same time I am writing <h1>test</h1> to the console. What I want is to write the word test only to the console. How do I do that? Nothing I tried works.

Thanks, Jim

Upvotes: 0

Views: 71

Answers (3)

Allan Kimmer Jensen
Allan Kimmer Jensen

Reputation: 4389

Use the .text() function, it will give you the text only.

$(function() {
  $("h1").click(function(){
    console.log($(this).text());
    $("p").toggle();
  });
});

You can see the working code here.

Upvotes: 3

Phil
Phil

Reputation: 11175

This should work:

console.log($(this).html());

Upvotes: 0

iambriansreed
iambriansreed

Reputation: 22241

Replace:

console.log((this));

With

console.log($(this).text());

Upvotes: 1

Related Questions