Reputation: 81
I have some trouble with filtering in jQuery:
<div>content: content: {"bla":"test","IMPORTANT"}</div>
<button>Remove</button>
<script>
$("button").click(function () {
$("div").remove(":contains('content: content: {\"bla\":\"test\",\"')");
});
</script>
So, onClick the whole div is being removed, I only want to remove the specified part, how can that be done?
Thanks!
Upvotes: 1
Views: 193
Reputation: 11779
If you want to remove what's inside the div then select the div and then set its contents to the empty string using text()
.
$("div").text("");
EDIT:
Apparently you want to keep part of the text which is there. To do that, you can use a regular expression to capture just the text that you want. To build off of Mark's answer:
$div = $("div");
str = $div.text()
str = str.replace(/.*?,"(.*)"\}$/, "$1");
$div.text(str)
Here's a jsfiddle
Upvotes: 2
Reputation: 4678
If the content of the div always comes in the same fashion, the following code will make a solution
$("button").click(function () {
var string = $("div").text();
var arr = string.split(":");
var arr1 = arr[3].split(",");
var string1 = (arr1[1].replace("\"","")).replace("\"}","");
$("div").text(string1);
});
Check the demo in Fiddle
EDITED FIDDLE: http://jsfiddle.net/2Gcy9/1/
Upvotes: 1
Reputation: 1611
$("button").click(function () {
$("div:contains('content: content: {\"bla\":\"test\",\"')").text("");
});
Upvotes: 0
Reputation: 5720
You will need to do some string manipulation on the contents of that div. Then update the text of the div with your results. Example:
$div = $("div:contains('content: content: {\"bla\":\"test\",\"')")
str = $div.text()
str = str.replace(/your regex goes here/, "")
$div.text(str)
Upvotes: 2