Remove blankspace from td class with greasemonkey - javascript

If I have webpage with for example alot of <td class="house">111 22</td>. The numbers are random but classname is the same and the blankspace between the numbers are at same position in all of them, and I want to remove the space in all of them after page is loaded. How should the script look like to work?

How about using this
var str = "111 22";
str = str.replace(" ","");
code example :
<script type="text/javascript">
//Page Initial
$(function() {
removeSpace();
});
function removeSpace(){
var str = $(".house").html();
str = str.replace(" ","");
$(".house").html(str);
}
</script>

Do this:
var elems = document.querySelectorAll('td.house');
for(var i = 0 ; i < elems.length; i++){
elems[i].textContent = elems[i].textContent.replace(" ","");
}

use the following code:
<script type="text/javascript">
window.onload = removeSpace;
function removeSpace() {
var emt = document.getElementById("mytable");//mytable is the Id of the table
d = emt.getElementsByTagName("tr")[0],
r = d.getElementsByTagName("td");
for (var i = 0; i < r.length; i++) {
var str = r[i].innerText;
r[i].innerText = str.replace(" ", "");
}
}
</script>

Related

How not to make <br> a tag

<script>
var y="<br>"
function repeater(num){
for (var i=0;i<num;i++)
{document.write(y) }
}
document.write(repeater(4))
</script>
My goal is to make the tag appear
4 times, not the actual breaks. The result shows undefined.
Since the question is asking for Javascript solution not JQuery (since no jQuery tags), I'll add the answer using Javascript.
<script>
var y = "<br>"
function repeater(num) {
for (var i=0; i < num; i++) {
document.body.innerText += y;
}
}
repeater(4);
</script>
or maybe you need to set a text into the spesific element (<div id="app"><div>)? no problem, we can improve the code a little bit.
<script>
var y = "<br>"
function repeater(el, num) {
for (var i=0; i < num; i++) {
el.innerText += y;
}
}
var el = document.getElementById('app');
repeater(el, 4);
</script>
<script>
var res = "";
var y="<br>";
function repeater(num){
for (var i=0;i<num;i++) {
res = res + y;
}
$('#customDiv').text(res);
}
(repeater(4))
You can put a custom <div id="customDiv"></div> inside your html file.

Add and remove separators to urls by javascript

I have links on page and want mask them. I want,
that at onLoad all points in urls (which are in href) will be replaced with something like "|",
so instead of
link
there is somehting like
link.
Then, at onClick replacements should be reversed to make links working again.
Need help to get this code to work:
function mask() {
var str = document.getElementByName("a");
var x = str.attributes.href.value.replace('.', '"|"');
document.getElementsByTagName("a").attributes.href.value = x;
}
function unmask(){
var str = document.getElementByName("a");
var x = str.attributes.href.value.replace('"|"', '.');
document.getElementsByTagName("a").attributes.href.value = x;
}
<body onLoad="mask()">
link
</body>
You have to use the getElementsByTagName method:
function mask() {
var a = document.getElementsByTagName('a');
for (var i = 0; i < a.length; i++) {
a[i].attributes.href.value = a[i].attributes.href.value.replace(/\./g, '"|"');
}
}
function unmask() {
var a = document.getElementsByTagName('a');
for (var i = 0; i < a.length; i++) {
a[i].attributes.href.value = a[i].attributes.href.value.replace(/"\|"/g, '.');
}
}
<body onLoad="mask()">
link
</body>
There are several issues in your code:
document.getElementByName("a") is not valid
str.attributes.href.value is not valid
You need to go global replace to replace all the . with | and vice-versa.
function mask() {
var str = document.getElementsByTagName("a")[0];
var x = str.href.replace(/\./g, '"|"');
str.href = x;
}
function unmask(){
var str = document.getElementsByTagName("a")[0];
var x = str.href.value.replace(/"|"/g, '.');
str.href = x;
}
<body onLoad="mask()">
link
</body>

How to conditionally change the table th tag text content

I am new to JavaScript & jQuery. I want to conditionally change the th text based on the content of an array.
var categoryNames = "Category : 1~!_Basis~#Category : 2~!_QUALITÄT~#Category : 3~!_GRUNDDATEN~#Category : 4~!_DISPOSITION~#Category : 5~!_VERTRIEB~#Category : 6~!_EINKAUF~#Category : 7~!_BUCHHALTUNG~#Category : 8~!_Optionen";
var replacetext = ["_Basis","_QUALITÄT","_GRUNDDATEN","_DISPOSITION","_VERTRIEB","_EINKAUF","_BUCHHALTUNG","_Optionen" ];
var res = categoryNames.split("~#");
for (var i=0; i< res.length; i++){
var pattern = res[i];
var rep = replacetext[i];
$(function(){
alert(pattern);
var m= $('table tr th:contains("'+pattern+'")');
m.text(rep);
});
}
Above code is just replacing for the last pattern which is "Category : 8~!_Optionen" by "_Optionen". Please suggest.
Don't include document.ready inside for loop.
var categoryNames = "Category : 1~!_Basis~#Category : 2~!_QUALITÄT~#Category : 3~!_GRUNDDATEN~#Category : 4~!_DISPOSITION~#Category : 5~!_VERTRIEB~#Category : 6~!_EINKAUF~#Category : 7~!_BUCHHALTUNG~#Category : 8~!_Optionen";
var replacetext = ["_Basis", "_QUALITÄT", "_GRUNDDATEN", "_DISPOSITION", "_VERTRIEB", "_EINKAUF", "_BUCHHALTUNG", "_Optionen"];
var res = categoryNames.split("~#");
for (var i = 0; i < res.length; i++) {
var pattern = res[i];
var rep = replacetext[i];
alert(pattern);
//var m = $('table tr th:contains("' + pattern + '")');
//m.text(rep);
alert(rep)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I am not sure what you are trying to do by wrapping the code block that is replacing the pattern in a jQuery object. The for loop can simply do the replacement.
for (var i=0; i< res.length; i++)
{
var pattern = res[i];
var rep = replacetext[i];
alert(pattern);
var m = $('table tr th:contains("'+pattern+'")');
m.text(rep);
}

Need to remove hyphen in the array

I have an array with some elements in it.
var arr=["bet-vet","ban-van","baskat-vase"];
While displaying the array, the want the hyphen in between the words to be removed. When it is being displayed,
the user should see
Bet,vet,ban,van,baskat,vase
Here is the link of my code
You ca try:
var arr = ["bet-vet", "ban-van", "baskat-vase"];
arr= arr.join("-").split("-").join();
console.log(arr);
Use Javascript Joint() & Split() method
example:
var arr=["bet-vet","ban-van","baskat-vase"];
var arrJoin = arr.join("-");
var arrsplit = arrJoin.split("-");
console.log(arrsplit);
You can try this
<script>
var b_or_v =["bet-vet","bat-vat","reb-rev","bick-vic","ban-van"];
function removeHyphen()
{
var newArray = [];
for(i=0;i<b_or_v.length;i++)
{
res = b_or_v[i].split("-");
newArray.push(res[0]);
newArray.push(res[1]);
}
console.log(newArray);
document.getElementById("text").innerHTML = newArray;
}
</script>
<body>
<button onclick="removeHyphen()">Find Val</button>
<div id="text">
</div>
</body>
Here is jsFiddle link
Try to use code as mentioned below:
document.getElementById("btn-1").onclick= function(){
document.getElementById("submit").onclick= function(){
var txt = [];
for (var i = 0; i < b_or_v.length; i++) {
if(b_or_v[i].match(/-/g)){
txt += b_or_v[i].split('-');
txt += ', ';
}
else{
txt += b_or_v[i]+', ';
}
}
Output: b_or_v : bet, vet, bat, vat, reb, rev, bick, vic, ban, van,
What about something like this:
var arr = ["bet-vet", "ban-van", "baskat-vase"];
var arr1 = [];
for(var i= 0; i < arr.length;i++){
test= arr[i].replace('-', '');
arr1.push(test);
}
console.log(arr1.join(', '));

Display maximum 5 words in each title using only JS

This is a code, ive used to maximize 5 words in each title
<script>
jQuery(document).ready(function() {
var limitWord = jQuery(".description").html();
limitWord.val(limitWord.val().substr(0, 5));
});
</script>
You can use the split function,
I have created a simple jsFiddle however it can be improvemed :)
jsFiddle : https://jsfiddle.net/nbj8ch45/
html
<div class="title">
this is a title with many different words
</div>
jquery
$(function()
{
var text = $('.title').text();
var words = text.split(' ');
$('.title').text(words[0]+' '+words[1]+' '+words[2]+' '+words[3]+' '+words[4]);
});
Here you are your example working
http://jsfiddle.net/2ad3cb3p/5/
var limit = 5;
$(document).ready(function() {
var descs = $(".description");
descs.each(function() {
var txt = $(this).html().split(" ");
var newTxt = '';
for(var i = 0; i < limit ; i++) {
newTxt += txt[i] + " ";
}
$(this).html(newTxt);
});
});
this might help you
jQuery(document).ready(function() {
var limitWord = jQuery(".description").html();
limitedWord = getWords(limitedWord);
});
function getWords(str) {
return str.split(/\s+/).slice(1,6).join(" ");
}
You must use split() function
<script>
jQuery(document).ready(function() {
var limitWord = jQuery(".description").html();
var res = str.split(" ");
});
</script>
In res[0] to res [4], you have the 5 words, see in:
http://www.w3schools.com/jsref/jsref_split.asp
<script>
jQuery(document).ready(function() {
var limitWord = jQuery(".description").html();
limitWord.val(limitWord.val().split(' ').slice(0, 5).join(' '));
});
you can try this
this should work:
<script>
jQuery(document).ready(function() {
var limitWord = jQuery(".description").html();
var arr = limitWord.split(' ')
var str = "";
for(var i=0; i<arr.length && i<5; i++){
str += arr[i] + " ";
}
jQuery(".description").html(str)
});
</script>
heres a fiddle: http://jsfiddle.net/shLyLc0k/

Categories

Resources