Capture text within a <td> via jQuery

Asked

Viewed 4,737 times

-1

How do I get only the amount (R$11,20)?

<td class="monetary" databind = "text: value label, visible: $parent.isShippingKnown()">R$11,20 </td>

It didn’t work, and there’s this other structure, I just need the text: "R$ 8,62 - Up to 8 business days"

<span data-bind="text: maxEstimateOptionText">Econômica - R$ 8,62 - Até 8 dias úteis</span>


$('#Econômica').text(); // Retorna
$('#Convencional).text(); // Retorna
$('#Rápida).text(); //Retorna
$('#Entrega Agendada I Convencional').text(); // Não Retorna
$('#Entrega Agendada I Sábado').text(); // Não retorna


<li data-bind="attr: {'class': idAttr}, css: { selected: name == $parent.selectedSlaName() }" class="seller-BTP-sla-EntregaAgendadaISabado">
    <a href="javascript:void(0)" data-bind="click: function() { $parent.selectedSlaName(name) }, attr: { 'id': name }" id="Entrega Agendada I Sábado">
        <i class="icon-ok" data-bind="visible: name == $parent.selectedSlaName()" style="display: none;"></i> &nbsp;
        <span data-bind="text: maxEstimateOptionText">Entrega Agendada I Sábado - A partir de R$ 76,20 - Primeira data em 06/08/2016</span>
    </a>
</li>

<a href="javascript:void(0)" data-bind="click: function() { $parent.selectedSlaName(name) }, attr: { 'id': name }" id="Entrega Agendada I Sábado">
    <i class="icon-ok" data-bind="visible: name == $parent.selectedSlaName()" style="display: none;"></i> &nbsp;
    <span data-bind="text: maxEstimateOptionText">Entrega Agendada I Sábado - A partir de R$ 76,20 - Primeira data em 06/08/2016</span>
</a>
<i class="icon-ok" data-bind="visible: name == $parent.selectedSlaName()" style="display: none;"></i> &nbsp;
<span data-bind="text: maxEstimateOptionText">Entrega Agendada I Sábado - A partir de R$ 76,20 - Primeira data em 06/08/2016</span>
  • Do you have access to the server code that generates this HTML? so you could pass it in an attribute data- and it was much safer and easier.

  • Just a curiosity: If you want to capture the text from within a select only from the one that was selected the code would be as follows: var text = $('#id_do_select :Selected'). text();

3 answers

0

I only need the text: "R$ 8,62 - Up to 8 business days"

$(document).ready(function(){
  var span = $("span").text();    
  var dados = span.split("Econômica - ");
  console.log(dados[1]);
  alert(dados[1]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span data-bind="text: maxEstimateOptionText">Econômica - R$ 8,62 - Até 8 dias úteis</span>

0

I don’t know what the rest of the HTML structure looks like, but a code like this solves for the case presented:

$("td.monetary").text();

By the structure it seems that you are using knockoutjs, if there are other options.

  • I am using the knockout library-2.3.0.js yes Douglas. Can you help me?

  • I am using the knockout library-2.3.0.js yes Douglas. Can you help me?

  • I don’t know what the structure of your Model for Knockoutjs is, but you can directly access the value from your model.(your object). maxEstimateOptionText(). This for the second case.

  • Could you put more information? The first case is within a data-bind="foreach: " ?

  • @Brunonascimento, I need a better example of your HTML, I’ve seen your edit, but I don’t know what are the Ids you’re using in your elements.

  • I improved the description @Douglas Fernandes, I was just structuring, I think it’s now complete. As I said, I couldn’t select the scheduled deliveries. I’m sorry for the mess, if you need me to edit, you can talk. Thanks in advance!

  • @Brunonascement, you can try to make something of this type $(".seller-BTP-sla-Deliverymantsaturday span"). text(); . But I don’t think that’s the case. By your logic you can also try something like $(".seller-BTP-Delivery Schedulesaturday.Selected span"). text();

  • Or you can try to do something like this in Knockout: var Textexpected; foreach( var i in object.<Listodel>()){ if(object.<Listone of a model>()[i].name == $Parent.selectedSlaName(){ Expected text = object. <Playlist of a model>()[i]. maxEstimateOptionText(); break; } }

Show 3 more comments

0

Here is an example of how to take the text of an HTML element and manipulate it. In this example I took the text of the DIV and put it inside an Input.

I hope I’ve helped.

$(function(){
    var valorDaDiv = $(".divdemonstracao").text();    
    $("#inputdemonstracao").val(valorDaDiv);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="divdemonstracao">Produto teste 1</div>

<br><br>

Texto vai aparecer dentro do Input:<br>
<input type="text" id="inputdemonstracao">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.