javascript jquery remove span [Solved] jQuery Removing One Word of Text
Tuesday, March 5, 2019

javascript jquery remove span [Solved] jQuery Removing One Word of Text

Stack Overflow



  1. Log In
    Sign Up

  2. current community


    • Stack Overflow

      help
      chat

    • Meta Stack Overflow

    your communities

    Sign up or log in to customize your list.

    more stack exchange communities

    company blog

    • Tour

      Start here for a quick overview of the site

    • Help Center

      Detailed answers to any questions you might have

    • Meta

      Discuss the workings and policies of this site

    • About Us

      Learn more about Stack Overflow the company

    • Business

      Learn more about hiring developers or posting ads with us

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .

Jquery remove text from span

Ask Question


up vote
3
down vote

favorite

5

I’m new to JQuery and really struggling how to perform the query I want.

I’m using SharePoint, and the when using the External Data Column and set it to required field, the error message always appears until you fill in the data. Unlike other type columns where they only appear after you click OK/Save and not filled in the data. Therefore I need to remove the error message text just from these type of columns.

I assume I need to search for span within the .ms-error class that contains the words ‘External Data’ and hide it.

From using IE developer toolbar, I’ve identified the area.

 <table style="border-collapse: collapse;" border="0" cellSpacing="0" cellPadding="0"> <tbody> <tr> <td colSpan="3"> <span > Text - You must specify a value before using the Check button. You can also use Select button to choose External Data. </span> </td> </tr> </tbody>
</table>

Please can someone help me with the JQuery.

javascript jquery

share | improve this question

edited Mar 3 ’17 at 11:45

JH_

397213

asked Jun 26 ’12 at 12:33

Cann0nF0dder

3441411

add a comment  | 

2 Answers
2

active

oldest

votes


up vote
3
down vote

accepted

$('span.ms-error:contains("External Data")').hide();

If you know for sure that these span‘s are inside a certain table or a div then target it specifically inside those to make the script perform better.

eg.

$('.ms-usereditor span.ms-error:contains("External Data")').hide();

share | improve this answer

edited Jun 26 ’12 at 12:46

answered Jun 26 ’12 at 12:35

Moin Zaman

22.1k55970

  • i may be wrong, but unless you know the ID of their container this won’t perform any faster than with just the ms-error class selector
    –  jbabey
    Jun 26 ’12 at 12:39

  • @jbabey: it should make the selector do less work.
    –  Moin Zaman
    Jun 26 ’12 at 12:46

  • Thank you Moin. I swear what you put is what I tried. I did a copy and paste directly from here to my code, and it worked. Thank you.
    –  Cann0nF0dder
    Jun 26 ’12 at 12:55

  • @Paul: You’re welcome! It couldn’t have been the same I think. Maybe the double quote inside contains?
    –  Moin Zaman
    Jun 26 ’12 at 12:59

add a comment  | 


up vote
26
down vote

var spans = $('.ms-error');
spans.text(''); // clear the text
spans.hide(); // make them display: none
spans.remove(); // remove them from the DOM completely
spans.empty(); // remove all their content

share | improve this answer

answered Jun 26 ’12 at 12:38

jbabey

36.5k95788

  • 1

    .empty() and .text('') do the same thing
    –  Moin Zaman
    Jun 26 ’12 at 12:43

  • Thank you but this also removes the error messages from other fields that aren’t External data. Such as people picker.
    –  Cann0nF0dder
    Jun 26 ’12 at 12:54

  • @MoinZaman if the element only contains 1 text node, yes.
    –  jbabey
    Jun 26 ’12 at 12:54

  • i used .text(' '). Works great because it clears it from the DOM
    –  Casey Robinson
    Jan 29 ’16 at 16:46

add a comment  | 

Not the answer you’re looking for? Browse other questions tagged javascript jquery or ask your own question .

asked

6 years, 5 months ago

viewed

43,306 times

active

1 year, 9 months ago

Related

2443

Is there an “exists” function for jQuery?

2146

Add table row in jQuery

6961

How do I check if an element is hidden in jQuery?

5192

How do I remove a property from a JavaScript object?

3723

Setting “checked” for a checkbox with jQuery?

4065

How to check whether a checkbox is checked in jQuery?

2065

Get selected text from a drop-down list (select box) using jQuery

6590

How do I remove a particular element from an array in JavaScript?

1961

jQuery scroll to element

4523

“Thinking in AngularJS” if I have a jQuery background?

Hot Network Questions

  • What are these "pimples" on the jet engine for?

  • Close all tabs in Safari except for one

  • Has there ever been a major migration from the New World to the Old World? If not, why?

  • Company I work at has this "thing" we have to say. What is this "thing" called?

  • Are there any gambits involving sacrificing a piece?

  • What is the utilitarian purpose of artificial waterfalls?

  • What is the meaning of this gesture in the British parliament?

  • Trifid Cipher (without keyword)

  • How to use "rendere" in a sentence?

  • Does a client certificate identify the owner to unrelated websites?

  • Do industry professionals building games using UE4 use Blueprints at all, or do they use C++ exclusively?

  • How powerful would an always-on Truesight ability be for a PC?

  • How should I approach reverse engineering this text encoding?

  • Unavailable manager says ‘I’m always available’

  • Can this Wish effect be dispelled?

  • Isn’t supervisor bound to teach for saving time?

  • Can I board a UK train I am booked on half way with a mobile, advanced ticket?

  • How do microstrips actually represent lumped components?

  • Sugar solutions have a neutral pH in themselves, but it makes your body acidic. Why?

  • How to graph a logistic equation using tikz

  • What would be the necessary conditions to rain bubbles?

  • O(·) is not a function, so how can a function be equal to it?

  • Sidescrolling shooter where you rescue Vietnam POWs

  • With Jeremy Crawford taking over Acquisitions, Inc, will his rulings on the show be considered RAW?

more hot questions


question feed

default

Stack Overflow works best with JavaScript enabled

Tutorialspoint
Home
Q/A
Tools
Coding Ground
Current Affairs
UPSC Notes
Online Tutors
Code Examples
Whiteboard
Net Meeting
Articles

Tutorialspoint

Library
Videos
eBooks

Register
Login

  • Library
  • Videos
  • eBooks
  • Register
    Login

  • Comments
  • Edit Question
  • Edit Categories

How can I remove everything inside of a <div> using jQuery?

The following is my div. On button click, I want to remove the content inside div:

<div >
<p>Inside div- This is demo text.</p>
<p>Inside div- This is demo text.</p>
</div>
<span>
<p>Inside span- This is demo text.</p>
<p>Inside span- This is demo text.</p>
</span>

Which method should I use in jQuery to achieve this?

  Follow
Answer
2404

0
0

1 Answer

David Meador

David Meador
,
1406 Views

To remove everything inside a div element, use the remove() method. You can try to run the following code to remove everything inside a <div> element:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function()
    $(".btn").click(function()
        $("div#demo").remove();
    );
     
  );
</script>
</head>
<body>
<button >Hide the elements inside div</button>
<div >
<p>Inside div- This is demo text.</p>
<p>Inside div- This is demo text.</p>
</div>
<span>
<p>Inside span- This is demo text.</p>
<p>Inside span- This is demo text.</p>
</span>
</body>
</html>

Live Demo

Comments

Edit Answer
Report

0
0
Would you like to add a better answer?
Advertisements

img

img

img

img

img

img

Advertisements

Follow





Tutorials Point

© Copyright 2018. All Rights Reserved.

Newsletter


  • Cookies Policy
  • FAQ’s
  • Helping
  • Contact

Scott Lewis
Developer. Iconographer. Writer.

Published


Scott Lewis

in jQuery
|

Remove an Element from an HTML String with jQuery

While working on a project for work today, I encountered a problem that I apparently have never encountered before. What I thought was a very simple function call in jQuery turned out to be a bit more complicated. I needed to removed an HTML element from a string representation of an HTML snippet. jQuery doesn’t quite behave the way I expected and I had trouble finding a solution.

Attempt #1 (Incorrect)

[cc lang=”javascript” escaped=”true”]
var theString = “<p>A string <span>with a span in it</span></p>”;
var theResult = $(“span”, theString).remove().html();
// Returns: ‘with a span in it’
[/cc]

The code above will return the element I want to remove, not the original string with the element removed. Not quite what I want.

Attempt #2 (Incorrect)

[cc lang=”javascript” escaped=”true”]
var theString = “<p>A string <span>with a span in it</span></p>”;
var theResult = $(“span”, theString).remove().end().html();
// Returns: ‘A string’
[/cc]

The code above, at least, gets the reference right, but unfortunatley, jQuery returns the ‘inner’ HTML but what I need is the ‘outer’ HTML. For some reason, it strips the enclosing element and gives me back jut the contents of the element.

Attempt #3 (Incorrect)

[cc lang=”javascript” escaped=”true”]
var theString = “<p>A string <span>with a span in it</span></p>”;
var theResult = $(“span”, theString).remove().end()[0];
// Returns: (Object) [HTMLParagraphElement]
[/cc]

The code above, gives me back the correct result but as the wrong type. This code returns an HTMLParagraphElement object. I can easily append this return value to another element or the document body, but I started with a string and I want to get a string back.

The Solution

I ended up using a little bit of jQuery slight-of-hand to get the result I want. The second incorrect approach above actually does return what I want, but when I call the jQuery.html() method, it returns the innerHTML.
So my approach is to first append the element to a new (temporary) DIV, then call jQuery.html() on the DIV and so get back the full HTML string I started with, minus the element I removed.

[cc lang=”javascript” escaped=”true”]
;(function($)
$.strRemove = function(theTarget, theString)
return $(“<div/>”).append(
$(theTarget, theString).remove().end()
).html();
;
)(jQuery);
[/cc]

Now I can get exactly the result I expect with the code below.

[cc lang=”javascript” escaped=”true”]
var theString = “<p>A string <span>with a span in it</span></p>”;
var theResult = $.strRemove(“span”, theString);
// Returns: “<p>A string </p>”
[/cc]

Scott Lewis
Developer. Iconographer. Writer.

Published

  1. Nice code. I wondered, can you use the same method with replaceWith? I explain: I have a string containing multiple images I want to replace with their alt attribute.

    Your method can help, but I have some difficulties adapting it, since I think I must use the each() method…

    •  That’s a really good question about replaceWith. I don’t think you need to do what I’ve done here. The whole problem I was trying to solve was that I couldn’t get jQuery to return the string itself. It wants to return a reference to the element that was removed or to the parent element, which is logical but not what I needed. The replaceWith just swaps one element for another so you’re not really concerned with the contents of each element.

  2. LOL. Crazy! This is always when I ask someone else I find the solution. Here is maybe another solution concerning the problem you described without making a function; it works for me. You helped me a lot too. Thank you. :))

    var text = 'some string with &lt;span&gt; span tags &lt;/span&gt; in it';
    text = '&lt;div&gt;' +text + '&lt;/div&gt;'; 
    text = $(text).find('span').remove().end().html();
    • Hi Terence. What you’ve done here is essentially what I was doing programatically. That’s what this line does:

      $(“”).append($(theTarget, theString).remove().end()).html();

      You could do it either way. Thanks for your contribution to the discussion. I’m glad you found the article useful.

  3. Thanks a load! You really saved me many, many hours of trial and error thanks to you (and google)

    • Google: A programmers best friend. 🙂 Glad the code helped out.

Comments are closed.

  • Related Content by Tag
  • javascript
  • jquery
  • LinkedIn

This website uses cookies to improve your experience. By using this site you consent to their use. Click the “Read More” link for full details on the information we collect. Accept Read More
Privacy & Cookies Policy