How to use an hidden character in text string using JavaScript.There is one text string which i read and writes back with the hidden character in it (so rewritten text looks same as the original text though it comtains the hidden character), so that next time i read the text i can come to know that this text is aleady read as it contains the hidden character
Eg)
< html>
< body>
< div>
This is a simple text
< /div>
< /body>
< /html>
I am trying to parse the div and extract the contents of the div, and insert an hidden character to the text and rewrite the text to the div again using JavaScript.
I just want to know which hidden character should i use to insert into the text ?
How to write the hidden character into the text ?
Since you are using javascript why don't you just add a property to the div:-
var divs = document.getElementByTagName("div");
for (var i = 0, length = divs.length; i < length; i++)
{
if (!divs[i].hasBeenRead)
{
fnReadDiv(divs[i]);
divs[i].hasBeenRead = true;
}
}
To answer the question, keep an array of the divs that have been traversed:
var divsChecked = [];
//code that looks at the div
divsChecked.push(div.getAttribute('id'));
However I think that the method that you are using to traverse the items may not be correct with libraries like jQuery you could loop over each div in turn thereby you shouldn't ever see the same div twice unless you run the loop twice.
Related
I have this javascript/jquery code that deletes sections of a html file
var divs = document.getElementsByTagName('div');
var d = divs.length;
for (var i = 1; i < d; i++) {
if( $('.section'+i).innerHTML.includes("textfind")==true || $('.section'+i).innerHTML.includes("textfind2")==true){
$('.section'+i).remove();
}
}
I would like write the same function as vba code for word
I have this code so far
Dim i as Long
For each objSect in ActiveDocument.sections
if objSect.Range.Text like "textfind +i" Or "textfind2+i" then objSect.range.delete
Next objSect
how would I got about deleting those particular section where i find the strings
Can you please elaborate the question here?? And, you want to remove html or word elements using VBA??
EDIT :- You can create a variable and store an array of strings in it to find and replace. (note: If you want to just clear the text then, use an empty string in place of the 'replace' string.)
Refer to this link for VBA find and replace code VBA To find and replace a string in MS Word
RemoveTextfinders: will remove both of there elements
<div class="section1">textfind2</div>
<div class="section2"><input name="textfind2" value="" /></div>
But neither of these elements
<div class="section1" name="textfind2"></div>
<div class="">textfind2</div>
Sub RemoveTextfinders()
Dim d, divs
Set divs = document.getElementsByTagName("div")
For Each d In divs
If InStr(d.className, "section") And InStr(d.innerHTML, "textfind2") Then d.ParentNode.RemoveChild d
Next
End If
Update I combined the two if statements they where redundant.
InStr(d.className, "section") will evaluate to true if any of the div's class contain the "section".
This code ran fine for me. I must be mistaken about the structure of your html. Could you please post a sample of the html?
Both the $("<test") & document.createElement(",test") throws error due to < character associated to the text. I do not want to replace the character & wanted to see if there is option to create dom or jquery object using such text. I know replace will work but since the code is pre-existing & also since code is written such that it assume it can either have the simple text (textnode) or html tag (like span) hence this error is occuring as it fails to check if it is proper self closing html tag.
I am thinking of creating it to xml node & then check if the childnode is textNode or not before trying to create jquery object,however I am looking for suggestion & best approach to tackle such issue. I know replace of < will work & also there is no need to check for attributes of plain text but since the code is dynamic it sometimes retrieves plain text & some time it gives valid html tag that why this issue appears
I am not sure what your exact end goal is, but basically you need to do something like this:
function makeElemHack( str ) {
var div = $("<div>").html(str); //create a div and add the html
var html = div.html(); //read the html
if (!html.length) { //if the html has no length the str was invalid
div.html(str.replace(/</g,"<")); //escape the < like text should be
//div.text(str); //or you can just add it as plain text
}
return div; //with the div wraper
//return div.contents(); //without the div wrapper
}
var bd = $("body");
bd.append( makeElemHack("<p>Hello</p>") );
bd.append( makeElemHack("1<0") );
bd.append( makeElemHack("<booo") );
Well, just like my title says.
Is it possible to search through a whole page's HTML/CSS, and then replacing certain strings using JavaScript?
I tried to make something on my own but I'm doing it wrong.
var i, sig = document.getElementsByClassName('signaturecontainer');
for (i = 0; i < sig.length; i++)
{
var str = sig[i].innerHTML;
var n = str.replace(/< div style='250px;overflow:scroll;'/g, "< div > style='height:100%'");
}
Why I want to replace the string instead of using .innerHTML and just editing is due to that the div I want to change does not have an ID/Class.
This is what the line(s) I need changed:
<!-- edit note -->
<blockquote class="postcontent lastedited">
Last edited by X; Today at <span class="time">06:32 AM</span>.
</blockquote>
<!-- / edit note -->
<div style="height:250px;overflow: auto;"> // <--- This one.
<blockquote class="signature restore"><div class="signaturecontainer">text here</div></blockquote>
</div>
</div>
This line will give trouble:
var new = str.replace(/< div style='250px;overflow:scroll;'/g, "< div > style='height:100%'");
new is a reserved word. Change it to newstr, or whatever.
Your regex pattern is unlikely to find anything: what it's searching for is not valid CSS
The string you want to insert is not valid HTML/CSS. It should probably be <div style='height:100%;'>
It occurs to me that if you're just tweaking the styling then this is a clumsy way to go. You can change the styling directly with Javascript.
var i, sig = document.getElementsByClassName('signaturecontainer');
for (i = 0; i < sig.length; i++)
{
sig[i].parentNode.style.height = "100%";
}
We'll no it is not impossible, with JavaScript you can parse an HTML document by locating the tags is that you are trying to change. For example
<div id="getThisChanged">change this text</div>
By executing this next line of JavaScript, you can change the text inside of the tag
document.getElementById('getThisChanged').innerHTML("text is now changed");
The text will be changed to "text is now changed"
i am writing some code which need to access a div which contains the particular text
following is the small portion of code i am working on:
var txtElem = txtdiv.getElementsByTagName("div");
txtElem[9].style.border = "2px solid blue";
as seen above i am accessing particular div with the index number, but now i want to add more code which can return me index of div from txtElem which contains the selected text from page
You need to loop trough the divs and check the contents with innerHTML
Try the code below
var txtElem = txtdiv.getElementsByTagName("div");
for ( var i = 0; i < txtElem.length; i++) {
if(txtElem[i].innerHTML === "The text in the div") {
//i is the index of the div that contains the text you searched on
alert(i);
}
}
Loop through divs and use JQuery .html() to check the html content of the div element.
For ease of use you may want to use jquery see http://api.jquery.com/contains-selector/
I'm trying to wrap each text character inside a
<div id="test"></div>
with a span tag.
No problem there, it's just that I also need to keep nested tags unmodified.
So if the string inside the DIV is:
"Some random text, <b>but</b> wait - there's tags inside!"
It should output this for the "but" part
<b><span>b</b><span>u</span><span>t</span></b>
Leaving the < b> and < a> tags alone, but wrapping all other chars in span. I checked text() but that doesn't seem to have a way of preserving tags or "saving back", only extracting the text and permanently removing the < b> and < a>
Maybe some jQuery guru out there knows how to do this?
You can do this by using the following code:
$("div").children().andSelf().contents().each(function(){
if (this.nodeType == 3) {
var $this = $(this);
$this.replaceWith($this.text().replace(/(\w)/g, "<span>$&</span>"));
}
});
See test case on jsFiddle.