Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value within a bold tag using Jquery

Tags:

jquery

I am having a table

<div class="main">
<table>
 <tr>
     <td>
         <b class="bold">1500></b>
     </td>
     <td>
        <b class="bold">2500></b>
     </td>
     <td>
        <b class="bold">4500></b>
     </td>
 </tr>
</table>
<input type="text" id="displayTotal"/>
<input type="button" id="btnAdd" value="Get Total"/>
</div>

Now on button click I want to add the values within bold tag having class name bold. I tried using

<script>
$('#btnAdd').click(function(){
var a=$("div.bold").Val();

I don't know what to do further. Anyone plz help out. I want the result as 8500 within textbox

like image 518
Zaker Avatar asked Jan 07 '15 12:01

Zaker


People also ask

How do I make part of a string bold in jQuery?

$(window). load(function() { // ADD BOLD ELEMENTS $('#about_theresidency:contains("cross genre")'). css({'font-weight':'bold'}); });

How do I get inner text in jQuery?

Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.

How do you represent the bold tag element?

The <b> tag specifies bold text without any extra importance.

What is text () in jQuery?

jQuery text() Method The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed).


1 Answers

Your html is not valid. b has to close. After you can use an iterator and using jquery text() you can get the values and sum them like:

$('#btnAdd').click(function() {
  //declare a variable to keep the values
  var sum = 0;
  //use each to iterate through b elements
  $("div.main table tr td b").each(function() {
    //sum the values
    sum += parseInt($(this).text(), 10);
  });
  //change input value with the new one
  $("#displayTotal").val(sum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
  <table>
    <tr>
      <td> <b class="bold">1500</b>

      </td>
      <td> <b class="bold">2500</b>

      </td>
      <td> <b class="bold">4500</b>

      </td>
    </tr>
  </table>
  <input type="text" id="displayTotal" />
  <input type="button" id="btnAdd" value="Get Total" />
</div>
like image 137
Alex Char Avatar answered Oct 21 '22 00:10

Alex Char