Kate Thompson
Kate Thompson

Reputation: 441

get value of inside a tag with jQuery.?

How can by jQuery get value inside tag b?

<span>
 <b>hi_1</b>
 <b>hi_2</b>
 <b>hi_3</b>
 <b>hi_4</b>
<span>

I want this output with jQuery: hi_1, hi_2, hi_3, hi_4

Please give me example in jsfiddle.

Upvotes: 1

Views: 12419

Answers (3)

JaredPar
JaredPar

Reputation: 755577

To get the value inside a specific HTML tag in jQuery you can use the text function. This combined with a selector gets the output you're looking for

$('span b').each(function() {
  console.log($(this).text());
});

JSFiddle

JSFiddle with commas

Upvotes: 3

mplungjan
mplungjan

Reputation: 178421

This is cool

var x = $("span b").map(function() {
  return $(this).text();
}).toArray().join(", "); 

Demo here

Discussed here

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

Are you looking for something like this?

http://jsfiddle.net/ZDYnq/

$(document).ready(function() {
   var textArr = [];
   $('span b').each(function() {
     textArr.push($(this).text());
   });
    alert(textArr.join(', '));
});

Upvotes: 3

Related Questions