I'm creating a really Find/Replace System but one of the main features are not working.
What's Supposed to happen:
Once you search all words found will highlight on the page. I want it so you can click it and it opens an Div saying: Replace {WORD HERE} with {INPUT} and then you can hit replace and it will replace that word with the text in the Input.
I'm using the findAndReplace Plugin and I don't want to change that.
What wont work:
Once you click on the word, the box opens, but I dont know how to make the found text replace with the one in the Input. Some of my code is in an One Line formate because I have:
return 'Code Here';
My Javascript:
shortcut.add("Ctrl+F", function() {
$('#finder').animate({
'bottom': '-53px'
}, 100);
});
shortcut.add("Shift+F", function() {
$('#finder').animate({
'bottom': '0px'
}, 100);
});
shortcut.add("Ctrl+C", function() {
$('#finder').animate({
'bottom': '-150px'
}, 100);
});
function findAndReplace(searchText, replacement, searchNode) {
if (!searchText || typeof replacement == '') {
$('.r').css({
'background': 'white',
'color': 'black'
});
return;
}
if (!searchText || typeof replacement === 'undefined') {
alert('No Items Found');
$('.r').css({
'background': 'white',
'color': 'black'
});
return;
}
var regex = typeof searchText === 'string' ? new RegExp(searchText, 'g') : searchText,
childNodes = (searchNode || document.body).childNodes,
cnLength = childNodes.length,
excludes = 'html,head,style,title,link,meta,script,object,iframe';
while (cnLength--) {
var currentNode = childNodes[cnLength];
if (currentNode.nodeType === 1 && (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
arguments.callee(searchText, replacement, currentNode);
}
if (currentNode.nodeType !== 3 || !regex.test(currentNode.data)) {
continue;
}
var parent = currentNode.parentNode,
frag = (function() {
var html = currentNode.data.replace(regex, replacement),
wrap = document.createElement('div'),
frag = document.createDocumentFragment();
wrap.innerHTML = html;
while (wrap.firstChild) {
frag.appendChild(wrap.firstChild);
}
return frag;
})();
parent.insertBefore(frag, currentNode);
parent.removeChild(currentNode);
}
}
$('#find').submit(function() {
findAndReplace(document.getElementById('fText').value, function(hi) {
var n = Math.floor(Math.random() * 9999999999);
var o = Math.floor(Math.random() * 9999999999);
var c = 'background:white;color:black;cursor:default;';
var id = 'changer' + n + '';
var onclick = "$('#replace_box" + n + "').slideDown();$('#black" + n + "').show();";
var close = "$('#replace_box" + n + "').remove();$('#black" + n + "').remove();$('#highlight" + n + "').css({'background' : 'white', 'color':'black'});";
var click = "$('.black').hide();$('#replace_box" + n + "').slideUp(900).delay(4000).remove();$('#highlight" + n + "').html('<span id=" + id + " style=" + c + "></span>');"
return '<div id="black' + n + '" class="black"></div><span id="highlight' + n + '" class="r" style="background: yellow;color: black;cursor:pointer;position:relative;" onclick="' + onclick + '">' + hi + '<div id="replace_box' + n + '" class="box" style="position:absolute;top:77px;left:116px;"><div style="position:relative;"><div class="close" onclick="' + close + '">Close</div>Replace <b>' + hi + '</b> with <input id="input' + n + '" autocomplete="off"/><br><br><button id="buttons' + n + '" onclick="' + click + '">Replace!</button><div class="chat-bubble-arrow"></div><div class="chat-bubble-arrow-border"></div></div></div></span>';
});
return false;
});
$('#replace').submit(function() {
findAndReplace(document.getElementById('fText').value, function() {
var mon = $('#rText').val();
return '<span class="highlight2" style="background: white;color: black;">' + mon + '</span>';
});
return false;
});
Oh, and I'm using the Shortcut plugin to make the find Box show on CTRL+F (Replacing the Browser Find System)
Note in the Javascript this is the main code for the Find:
$('#find').submit(function() {
findAndReplace(document.getElementById('fText').value, function(hi){
var n = Math.floor(Math.random() * 9999999999);
var o = Math.floor(Math.random() * 9999999999);
var c = 'background:white;color:black;cursor:default;';
var id = 'changer'+n+'';
var onclick = "$('#replace_box"+n+"').slideDown();$('#black"+n+"').show();";
var close = "$('#replace_box"+n+"').remove();$('#black"+n+"').remove();$('#highlight"+n+"').css({'background' : 'white', 'color':'black'});";
var click = "$('.black').hide();$('#replace_box"+n+"').slideUp(900).delay(4000).remove();$('#highlight"+n+"').html('<span id="+id+" style="+c+"></span>');"
return '<div id="black'+n+'" class="black"></div><span id="highlight'+n+'" class="r" style="background: yellow;color: black;cursor:pointer;position:relative;" onclick="'+onclick+'">' + hi + '<div id="replace_box'+n+'" class="box" style="position:absolute;top:77px;left:116px;"><div style="position:relative;"><div class="close" onclick="'+close+'">Close</div>Replace <b>'+hi+'</b> with <input id="input'+n+'" autocomplete="off"/><br><br><button id="buttons'+n+'" onclick="'+click+'">Replace!</button><div class="chat-bubble-arrow"></div><div class="chat-bubble-arrow-border"></div></div></div></span>';
});
return false;
});
Here is my HTML:
<div id="finder">
<div style="position:relative;">
<form id="find" style="padding-bottom:10px;">
<button class="close2" id="wa" onclick="$('#finder').animate({'bottom' : '-150px'}, 100);">X</button>
<input id="fText" placeholder="Enter Text you wanna replace here!" autocomplete="off" style="width:210px;"/>
<button>Find!</button>
</form>
<form id="replace">
<input id="rText" placeholder="Replace all of the found items." autocomplete="off" style="width:210px;"/>
<button>Replace All</button>
</form>
</div>
</div>
<div class="black"></div>
<div id="show"></div><div id="test"></div>
<div id="boxes"></div>
I also have CSS But I won't be posting that here.
My Example Is here:
Find/Replace
Really hope someone can understand the way I code and can help.
Here is your code for the "Replace" button that appears when you click on a highlighted word:
<button id="buttons7430059098" onclick="$('.black').hide();$('#replace_box7430059098').slideUp(900).delay(4000).remove();$('#highlight7430059098').html('<span id=changer7430059098 style=background:white;color:black;cursor:default;></span>');">Replace!</button>
What it is doing is replacing the contents of the highlighted element with an empty span. You need to actually put whatever the user typed into the input field ($("#input7430059098").val()
) into that span.
I would also create a function that can be called from the click handler of the button to do the replacement, as there already seems to be a lot of code right in the click handler, but that's just me.
Edit:
Try this:
var click = "$('.black').hide();$('#replace_box"+n+"').slideUp(900).delay(4000).remove();$('#highlight"+n+"').html('<span id="+id+" style="+c+">' + $('#input" + n + "').val() + '</span>');";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With