value of text.replace() to another variable - javascript

I have a code to clear and paste text after clicking a button. This text is pasted into two different textareas and therefore my code has to clean up the intercepted content a bit differently. The problem is that it doesn't work to pass the content to another variable...
$(document).on('ready', function() {
$('.quoteMsg').click(function() {
var txt = $(this).closest('.replyBox').find('.replyMsg').html().trim();
var txtau = $(this).closest('.replyBox').find('.replyMsgau').text().trim();
txt = txt.replace(/(?:\r\n|\r|\n)/g, ' ');
txt = txt.replace(/<p class="c0">/gi, '');
txt = txt.replace(/<\/p>/gi, '');
txt = txt.replace(/</g, "<");
txt = txt.replace(/>/g, ">");
txt = txt.replace(/&/g, "&");
txt = txt.replace(/"/g, '"');
txt = txt.replace(/'/g, "'");
var txtq = txt; //it doesn't work to pass the whole value to the txtq variable
//txtq = txt; //also not working
txtq = txt;
txtq = txt.replace(/<blockquote>/gi, '');
txtq = txt.replace(/<\/blockquote>/gi, '[hr][i]' + txtau + '[/i]: ');
var txte = txt; //it doesn't work to pass the whole value to the txte variable
//txte = txt; //also not working
txte = txt.replace(/<blockquote>/gi, '[quote]');
txte = txt.replace(/<\/blockquote>/gi, '[/quote]\n');
txte = txt.replace(/<blockquote>/gi, '');
$("textarea[name='tresckomentapisz']").val('[quote]' + txtq + '[/quote]\n' + txtau + ', ');
$("textarea[name='editkomtxt']").val(txte);
});
});
I want txtq and txte to format the value differently for <blockquote>, but data transfer from txt doesn't work - why?
I need this efect for <quote>:
console.log(txtq + '|' + txte + '|' + txt);//[hr][i]etc[/i] | [quote]\n
but is working like that:
console.log(txtq + '|' + txte + '|' + txt);//<blockquote>|<blockquote>|

For txtq and txte, you are modifiying txt var.
Insted of:
txtq = txt.replace(/<blockquote>/gi, '');
try:
txtq = txtq.replace(/<blockquote>/gi, '');

Related

Why can't I get the value of this var?

I want to create a live change field input system with jQuery. This is my code:
$(function() {
$('td').on('dblclick', function() {
var tdValue = $(this).text();
var tdTag = $(this).html();
tdTag = '<input id="newClass" type="text" value="' + tdValue + '">';
$(this).html(tdTag);
$('input#newClass').on('change', function() {
var inputVal = $(this).val();
alert('inputVal = ' + inputVal);
var inputTag = $(this).html();
alert('inputTag = ' + inputTag);
var var3 = '<span> var3 ' + inputTag + '</span>';
alert(var3);
})
})
})
Why is inputTag and var3 empty? This is my (very simple) html code
Note that the td tags are repeated using a PHP while loop. Thank you in advance.
Now I don't have enough reputation to comment and I'm still confused. Try following codes and see if anything suits you.
var inputTag = $(this)[0].outerHTML;
or
var inputTag = $(this).parent()[0].outerHTML;
or
var var3 = '<span>' + inputVal + '</span>';
$(this).after(var3);
$(this).remove();
or if you just want to make the input field read only, try with
$(this).prop("readonly", true);

javascript line break is not applying when i use document.execCommand("copy")

Hi i'm using below code to build a string and copying it,but in output when i paste it the line break is not applying
function copyToClipboardShipto() {
var $temp = $("<input>");
$("body").append($temp);
var str1 = "#(Model.firstName)"; var str2 = " "; var str3 = "#(Model.lastName)"; var str4 = "\n";
var str5 = "#(Model.shiptoes[0].address.address1)";
var str6 = ",";
var str7 = "#(Model.shiptoes[0].address.address2)";
var str8 = "\n";
var str9 = "#(Model.shiptoes[0].address.city)"; var str10 = ","; var str11 = "#(Model.shiptoes[0].address.state)"; var str12 = "\n";
var str13 = "#(Model.shiptoes[0].address.zip)";
var str = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11 + str12 + str13;
$temp.val(str).select();
document.execCommand("copy");
$temp.remove();
}
}
firstname lastname223 E JACKSON AVE,city,statezip
any help appericiated
Use <textarea> instead of <input>, since INPUT doesn't support multiline strings.
var $temp = $("<textarea>");
Use <textarea> instead as <input> wont support line breaks.

jquery Append add same value always

I use this jQuery code to append a div to another:
$('#sklep').on('click', '.kup', function () {
var cena = $('#cena').text();
var tytul = $('#tytul').text();
var iden = $('#id').text();
var suma = $('#koszykdiv .cyfry').text();
var dodawanie = parseInt(cena) + parseInt(suma);
$('.cyfry').text(dodawanie);
var source = $("#miniatura").attr("src");
$('.koszyk_content_items').append($('<div class="item_koszyk"><div class="miniatura_koszyk"><img width="165" height="110" src="' + source + '"/></div><span id="opis">' + tytul + ' - ' + cena + ' zł - </span><span class="identyfikator" style="display:none;">' + iden + '</span>USUŃ</div>'));
var licznik = $('#koszykdiv .licznik').text();
alert(licznik);
$('#identyfikator').text(licznik);
var licznik_dodawanie = parseInt(licznik) + 1;
$('#koszykdiv .licznik').text(licznik_dodawanie);
$.cookie("obraz" + licznik_dodawanie, id);
var cena = '';
var tytul = '';
var iden = '';
var source = '';
});
but it always appends a div with the same variables values, even if parent of '.kup' href has another text values. Can you help me and tell where the problem is?
You say in your question that you get the same variable values "even if parent of '.kup' href has another text values."
This is because nothing in your code is relative to the '.kup'-element. You get the src-attribute of the element with id=miniatura everytime.

Weird Javascript Bug in IE? for-loop string concatenation

var pre = '<a href=someDirectoryPath';
var mid = '.aspx">';
var post = '</a>';
var trailHTML = '';
for(i=0;i<trail.length;i++) {
trailHTML = trailHTML + pre + getURL(trail[i]) + mid + trail[i] + post;
if(i!=(trail.length-1)) {
trailHTML += ' > ';
}
}
document.write(trailHTML);
trail is an arraylist of valid pages, like so:
['some name', 'another name','yet another name','name']
getURL just takes that name and adds a '-' in between words, which is the page name. This has been tested and works. (for example, getURL('some name') returns 'some-name')
The problem is, when run in IE9 (untested in other browsers), when I write trailHTML to the page, I only get the last element in the array. Why is this?
Let me know if you need any clarification...
function getURL(txt){
return txt.replace(/ /g,"-");
}
var trail = ['some name', 'another name','yet another name','name'];
///////////////////////////////////////////////////
var pre = '<a href="someDirectoryPath/'; // changed
var mid = '.aspx">';
var post = '</a>';
var trailHTML = '';
for(i=0;i<trail.length;i++) {
trailHTML += pre + getURL(trail[i]) + mid + trail[i] + post;
if(i<trail.length-1)trailHTML+=" > " // changed
}
document.write(trailHTML);
You have a syntax error in your for-loop: The open "{" on the if does not have a matching "}".
I ran a small sample in IE9 and got all items in the array, not just the last item as you report. Here's what I ran:
<script type="text/javascript">
function getURL(s){
return s.replace(" ", "-");
}
var trail = ['some name', 'another name','yet another name','name'];
var pre = '<a href=someDirectoryPath';
var mid = '.aspx">';
var post = '</a>';
var trailHTML = '';
for(i=0;i<trail.length;i++) {
trailHTML = trailHTML + pre + getURL(trail[i]) + mid + trail[i] + post;
if(i!=(trail.length-1)) {
trailHTML += " > ";
}
}
trailHTML = trailHTML + getURL(trail[0]);
document.write(trailHTML);
</script>
The output looked like this:
some name > another name > yet another name > namesome-name
Most likely your issue is caused by the syntax error, or in how the array is built/passed into your function.

Using a list position as a param to a function to pull certain data

function getList()
{
var string2 = "<img src='close.png' onclick='removeContent(3)'></img>" + "<h4>Survey Findings</h4>";
string2 = string2 + "<p>The 15 Largest lochs in Scotland by area area...</p>";
document.getElementById("box3text").innerHTML = string2;
var myList = document.getElementById("testList");
for(i=0;i<lochName.length;i++)
{
if(i<3)
{
var listElement = "<a href='javascript:getLoch(i)'>" + "Loch "+ lochName[i] + "</a>";
var container = document.getElementById("testList");
var newListItem = document.createElement('li');
newListItem.innerHTML = listElement;
container.insertBefore(newListItem, container.lastChild);
}
else
{
var listElement = "Loch "+lochName[i];
var container = document.getElementById("testList");
var newListItem = document.createElement('li');
newListItem.innerHTML = listElement;
container.insertBefore(newListItem, container.lastChild);
}
}
}
This function generates a list with the 1st 3 elements being hyperlinks. When clicked they should call a function call getLoch(i) with i being the position of the item in the list. However when i pass it the value it just give it a value of 15, the full size of the array and not the position.
function getLoch(Val)
{
var str = "<img src='close.png' onclick='removeContent(4)'></img>" + "<h4>Loch " + lochName[Val] +"</h4>";
str = str + "<ul><li>Area:" + " " + area[Val] + " square miles</li>";
str = str + "<li>Max Depth:" + " " + maxDepth[Val] + " metres deep</li>";
str = str + "<li>County:" + " " + county[Val] + "</li></ul>";
document.getElementById("box4").innerHTML = str;
}
There are 2 errors in your code as far as I can see. The first is the way you create your link.
var listElement = "<a href='javascript:getLoch(i)'>" + "Loch "+ lochName[i] + "</a>";
This will actually result in code like this:
<a href='javascript:getLoch(i)'>Loch name</a>
Passing a variable i is probably not what you intended, you want it to pass the value of i at the time your creating this link. This will do so:
var listElement = "<a href='javascript:getLoch(" + i + ")'>" + "Loch "+ lochName[i] + "</a>";
So why does your function get called with a value of 15, the length of your list? In your getList function, you accidently made the loop variable i a global. It's just missing a var in your loop head.
for(var i=0;i<lochName.length;i++)
After the loop finished, i has the value of the last iteration, which is your array's length minus 1. By making i a global, and having your javascript code in the links use i as parameter, getLoch got called with your array length all the time.

Categories

Resources