0

How to remove some part of data from a variable

This is my jsfiddle

http://jsfiddle.net/9yvk50go/

When once clicked on submit , i want to remove this part of text

'<p class="tcPriceWrap">25</p>'

So that i can have toppings array with only these values

Honey with Chocolate Sauce  10 ML
Honey with Carmel  10 ML   

The below is the HTML

<div class="prd-items-detials">    
<div style="display: block;" class="Topping-details" id="4">
   <section id="topping_tsection_4">
      <aside>
         <h6 class="tdHeading">Quantity      1</h6>
         <section class="secclass">
            <a data-id="4" topping_id="1" id="4_ZZ_0_ZZ_0" topp_name="Honey with Chocolate Sauce  10 ML" top_price="25" class="tpActive">
               Honey with Chocolate Sauce  10 ML 
               <p class="tcPriceWrap">25</p>
            </a>
         </section>
         <section class="secclass">
            <a data-id="4" topping_id="2" id="4_ZZ_0_ZZ_1" topp_name="Honey with Carmel  10 ML" top_price="25" class="tpActive">
               Honey with Carmel  10 ML 
               <p class="tcPriceWrap">25</p>
            </a>
         </section>
      </aside>
   </section>


</div>

    <input type="button" id="someid" value="Submit"/>

  </div>

This is my javascript

var toppings = [];

$(document).on("click", "#someid", function(e) {

$("#topping_tsection_4").find('.tdHeading').each(function () {
                values = [];
                $(this).parent().find('.tpActive').each(function () {

                    alert($(this).text().trim());
                    values.push($(this).text().trim());
                });
                if(values.length>0)
                {
                    toppings.push({
                        'name': $(this).text().trim(),
                        'value': values
                    });
                }
            });


    });    // close of save event listener
5
  • You want them both removed? Commented Aug 8, 2014 at 14:25
  • yes correct , i want to eliminate the p tag from both . Commented Aug 8, 2014 at 14:26
  • 1
    So why not just use $('.tcPriceWrap').remove()? Commented Aug 8, 2014 at 14:26
  • Do you only want them removed from the alert or do you want them removed from the DOM? Commented Aug 8, 2014 at 14:27
  • Thanks , i want to remove from the alert only and not from the DOM . Commented Aug 8, 2014 at 14:30

3 Answers 3

1

Assuming that you want to keep that data in the DOM, I'll suggest this replacement in your code:

$(this).parent().find('.tpActive').each(function () {
  alert($(this).text().trim());
  values.push($(this).text().trim());
});

for this:

$(this).parent().find('.tpActive').each(function () {
  // Clone the element to prevent any change in the original DOM object.
  var $el = $(this).clone(); 
  // Remove what you want.
  $el.find('.tcPriceWrap').remove();

  alert($el.text().trim());

  values.push($el.text().trim());
});

I hope it helps you.

Sign up to request clarification or add additional context in comments.

Comments

0

Do this in your alert.

alert($(this).clone().children().remove().end().text().trim());

It's basically

.clone cloning the object

.children getting the children of the cloned object

.remove removing all the children of cloned object

.end going back to selection after that get the text

Comments

0

If I understood you correctly, you want something like this:

$("#submitid").click(function(){
    $(".tcPriceWrap").each(function(){$(this).remove();});
});

jsfiddleURL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.