Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value of p tag with jquery

Im using jquery to try to take the value text in a p tag.

I set up an alert for testing to give me the value and it says "current stack is [object Object]"

My html for this is

<div id="player9">
    <div class="player_stats">
      <p class="player_name"></p>
      <p class="player_action"></p>
      <p class="player_money"></p>
   </div>
</div>

My alert code is:

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money"));

if I add .val() it comes back as undefined. These values were set upon page load by:

$(whichseat[seat]).children(".hand").children("p.bet").text(bet);

"whichseat[seat] and bet are based into the function. They are setting correctly.

If I use firebug and checkout that specific p tag it says:

<p class="player_money">60000</p>

How do I correctly grab that value?

Any help would be greatly appreciated!

like image 316
mahle Avatar asked Dec 22 '22 16:12

mahle


2 Answers

You could use .html() or .text()

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money").html());

Or:

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money").text());

Obviously the text method will grab just the text, where as the html will get the markup as well. Demo Here

like image 75
Scoobler Avatar answered Dec 31 '22 11:12

Scoobler


You can use the text() method. The val() method is used for getting input field's values. So, you will have:

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money").text());
like image 35
Abdel Raoof Olakara Avatar answered Dec 31 '22 11:12

Abdel Raoof Olakara