Remove first word from a <td> - javascript

I have this code in my page and i want to remove first word and the minus with java script or jquery(in this case #123456 -)
<td id="someid"><a>#123456 - Some Text</a></td>
to look like this
<td id="someid"><a>Some Text</a></td>
Thank you!

Using str.substring split your string from the index of
'-' + 2 = Start of Second word to length of string
var str = document.getElementsByTagName('a')[0].innerHTML;
newstr = str.substring(str.indexOf('-')+2, str.length);
console.log(newstr);
<td id="someid"><a>#123456 - Some Text</a></td>

var str = $("#someid a").html();
$("#someid a").html( str.substring(str.search("-")+1, str.length));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="someid"><a>#123456 - Some Text</a></div>

You can do it by using jQuery
var col = $('#someid').html();
var anchor= col;
$('#someid').html('<a>'+anchor.split('-')[1].split('<')[0]+'</a>')
find the working fiddle example. https://jsfiddle.net/957keh1q/1/

Assuming the "-" char is fixed, split the string in two
var txtArray = $('#someid').val.split("-");
and take out the first space:
var newText = txtArray[txtArray.length - 1].trim();

Related

Count words in DOM string

I have the following HTML string:
<div><p>Hello <b>how are</b> you?</div>
I would like to loop the HTML string DOM and wrap each word with a span tag and the word number as id so result will be like this:
<div><p><span id="word-1">Hello</span> <b><span id="word-2">how</span> <span id="word-3">are</span></b> <span id="word-4">you?</span></div>
I've tried to use the JQuery method $.parseHTML but no luck to count the words because DOM node value can contain more than one word in it..
In addition, if inside the word there is inline tags such <b> / <i> so from DOM point of view each tag has a different node value even when its the same word)
Any idea how to solve this issue? how to count words inside a HTML DOM string?
Thanks
Try this.
HTML
<div id="content">
<div><p>Hello <b>how are</b> you?</div>
</div>
Script
var textNodes = $("#content *").contents().filter(function() {
return this.nodeType === 3;
});
var counter = 1;
for(var i = 0;i<textNodes.length;i++)
{
var val = $(textNodes).eq(i).text();
var words = val.split(" ");
var final = "";
for(var j = 0;j<words.length;j++)
{
if(words[j].trim() != "")
{
final += "<span id='"+ counter +"'>"+ words[j] +" </span>";
counter++;
}
}
$($(textNodes)[i]).replaceWith(final);
}
Jsfiddle Link
As of my understanding of your question this should work.
var allText = $("body").text().replace(/<[^>]*>/g, "");
var words = allText.split(' ');
for(var i=0;i<words.length;i++)
{
$(div).append("<span id = 'word-'"+i+">"+words[i]+"</span>")
}

How To Print Variable Data And String By Adding Inside Single Quotes

How To Output Variable data And String With Single Quotes.I Want To Add String After The Value Of mo.Output Should Be Inside Single Quotes.
Output Should Be:- '123target'
<button value="123" class="btn">Button 1</button>
$('.btn').click(function(){
var mo = $(this).val();
var re = '''+mo+'target'';
document.write(re);
});
Use double quote to wrap or escape it using \.
var re = '\'' + mo + 'target\'';
// or
var re = "'" + mo + "target'";
Try this,
$('.btn').click(function(){
var mo = $(this).val();
var re = "'"+mo + "target'";
document.write(re);
});
Check jsfiddle link.
You haven't kept quotes properly.
$('.btn').click(function() {
var mo = $(this).val();
var re = "'" + mo + "target'";
document.write(re);
});
<button value="123" class="btn">Button 1</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
additionally you can also use ES6 template strings (backtics)
var mo = "quotes"
out.innerHTML=`some string in 'single ${mo}' and some in "double ${mo}" and also some "mix'd ${mo} content"`
<div id="out"></div>

Remove the vertical spacers in a td

What is the best way to remove the last occurrence of the element spacer (|) from this td using jQuery?
<td class="actionCol" nowrap="">XYZ | ERR | PING</td>
So, to remove only the last occurrence, you may do the following.
$('.actionCol').html(function(_, elem) {
return elem.replace(/\|(?!.*\|)/, '');
});
A Demo
You can replace the last occurrence using substr. No need to use regex, this will be faster than regex.
$('.actionCol').html(function(i, e) {
var lastIndex = e.lastIndexOf('|');
return e.substr(0, lastIndex) + e.substr(lastIndex + 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<td class="actionCol" nowrap="">XYZ | ERR | PING
</td>
</table>

jQuery change text in link

I have a simple question for jQuery...
I have a table with a link like this
<table>
<tr>
<td class="views-field">
201105
</td>
</tr>
</table>
Now I would change the link's text from 201105 to 2011-05
(simple add a "-" after the first 4 characters)
I tried substring but don't work... Help me!!
This will translate all td.views-field links:
$('td.views-field a').each(function () {
var oldText = $(this).text();
$(this).text(oldText.substr(0,4) + '-' + oldText.substr(4));
});
$("a").text($("a").text().substring(0,4)+"-"+$("a").text().substring(4,6) );
Try the code below, that should work fine.
var replaceElement = $("td.views-field a").first();
var oldDate = replaceElement.text();
var newDate = oldDate.substring(0, 4) + "-" + oldDate.substring(4,6);
replaceElement.text(newDate);

Javascript .replace() / regex with code as string

I'm trying to "remove" (just replace it with "") the following String:
<script type="text/javascript">
document.createElement("img").src="http://www.google.com"
</script>
From the following String:
<SCRIPT LANGUAGE=JavaScript>
var PAswf = "_ADPATH_[%SWF File%]";
var advurl = "_ADCLICK_";
var PAadvurl = escape(advurl);
var PAwidth = "[%Width%]";
var PAheight = "[%Height%]";
var wmValue = "_ADDCP(wm)_";
if (!wmValue)wmValue = "opaque";
var PAwmode = wmValue;
document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
document.write(' WIDTH="'+ PAwidth +'" HEIGHT="'+ PAheight +'">');
document.write('<PARAM NAME=movie VAL'+'UE="' + PAswf + '?clickTag='+ PAadvurl +'"><PARAM NAME=quality VALUE=high><PARAM NAME=wmode VALUE='+ PAwmode +'>');
document.write('<EMBED sr'+'c="'+PAswf + '?clickTag='+ PAadvurl +'" quality=high wmode='+ PAwmode);
document.write(' swLiveConnect=TRUE WIDTH="'+ PAwidth +'" HEIGHT="'+ PAheight +'"');
document.write(' TYPE="application/x-shockwave-flash">');
document.write('</EMBED></OBJECT>');
</SCRIPT>
<script type="text/javascript">
document.createElement("img").src="http://www.google.com"
</script>
As you can see, the String is at the bottom of the code block, but that shouldn't really matter. I've stored the first string as a variable, codeString, and tried the following.
// code refers to the bigger code string
code.replace(/codeString/gm, "");
Doesn't seem to match. I'm sure I'm doing something wildly wrong, but I've searched for about an hour and have come up empty. Any ideas?
/codeString/ is a regular expression searching for the literal "codeString", not a variable containing a pattern. Also, string.replace(pattern, replacement) does not modify string but returns a new string with the replacement applied.
You do not need a regular expression for stripping fixed strings like this, the next code will remove the string once from code:
var codeString = '<script type="text/javascript">\n' +
'document.createElement("img").src="http://www.google.com"\n' +
'<\/script>';
code = code.replace(codeString, "");
Well you can start with
code.replace(codestring, "");
If it needs to be a regular expression, you can use:
code.replace(new RegExp(codestring, "gm"), "");
but be aware that the regex metacharacters should be escaped with backslashes.

Categories

Resources