How to Get a Value of a <Span> Using Jquery

How do I get the content of a span using jQuery?

I think this should be a simple example:

$('#item1 span').text();

or

$('#item1 span').html();

Get value of span with jquery

Note that only input has a value. Others you must use data attribute.

<span class="glyphicon glyphicon-remove" aria-hidden="true" th:attr="data-value=${cost.cost_id}"></span>

$(document).ready(function(){
$('.glyphicon.glyphicon-remove').click(function(){
var t = $(this).data('value'); // if int the use parseInt() function;
var t = paerseInt($(this).data('value'));
alert(t);
});});

How to get the value with span text by using jquery

You need to get the text content of span immediately after the element. Where use next() method to get the span element and use text() method to get the text content.

var $email = $("#emial");
var email = $email.val() + $email.next().text();

$('#email').on('input', function() {  var $email = $(this);  var email = $email.val() + $email.next().text();  console.log(email);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div class="input-group" style="width:40%">  <input type="text" name="email" id="email" class="form-control" required="true" />  <span class="input-group-addon">@gmail.com</span></div>

Get span value with jquery

The problem here is that you're assigning a jQuery object to your variable, and not the content of the element. To extract the text inside the <span>, you should use either .html() or .text(), and do this instead:

var availableCodes = $("#codesQuantity").text();

How to set a value for a span using jQuery

You can do:

$("#submittername").text("testing");

or

$("#submittername").html("testing <b>1 2 3</b>");

Javascript to get value from a span

1.Since span has id. So use # instead of .

2.Use .html() or .text() instead of .val().

Like below:-

var r_commentid = $("#astar").html();

A sample example:-

$(document).ready(function () {  $("#reply").click(function () {    var r_comment = $("#replytext").html();    var r_commentid = $("#astar").html();    console.log(r_comment);    console.log(r_commentid);        var r_comment1 = $("#replytext").text();    var r_commentid1 = $("#astar").text();    console.log(r_comment1);    console.log(r_commentid1);  });})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><span id="astar">1</span><span id="replytext">Hey changed jQuery code is working fine now!</span>
<button id="reply">Click Me!</button>

JavaScript/jQuery - Get value of span of current element

you shouldn't place tags inside option tag, that said try this

$('.config-option').each(function() { 
var currentElement = $(this);
var optionPrice = currentElement.find('.option-price').parent().text();
console.log(optionPrice);
});

updated: span tags get removed by browser so use this markup structure

<select>
<option data-price="PRICE_HERE">

then use this in js

$('.config-option option:selected').each(function() { 
var optionPrice = $(this).attr('data-price');
console.log(optionPrice);
});

Get the specific text inside span using jQuery

try using contents

$('.indent0').each(function() {
var celltext = $(this).contents().first()[0].textContent; //this line has changed
if (celltext == "Data1") {
var spantext = $(this).find(".taglist").html();
if (spantext != null) {
alert(spantext);
}
}
});

or

$('.indent0').each(function() {
var celltext = $(this).contents()[0].nodeValue; //this line has changed
if (celltext == "Data1") {
var spantext = $(this).find(".taglist").html();
if (spantext != null) {
alert(spantext);
}
}
});

How to get the value of span inside a td using Jquery?

$('.date-data span').text();

It'd be better to add a id.

Jquery - On input, Get the value of a span inside a td using span class

You select the element again with the variantId variable. But that is only a string and won't select the element, because the string doesn't contain the # when selecting elements with an id.

variantId = '#'+$(this).attr('id');

Now variantId will be #+your_id



Related Topics



Leave a reply



Submit