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>
Related
I want to remove the "http" if it is put in the url part of the input link, before the data is sent.
this my input code look onclick=
<input style=" outline: none;" type="button" onclick="formatText ('link:url');" class="btn btn-yeni" value="link"/>
This my javascript code (the received data is sent to another file and replaced.)
<script type="text/javascript">
function formatText(tag) {
var Field = document.getElementById('entry_girdi');
var val = Field.value;
var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
var before_txt = val.substring(0, Field.selectionStart);
var after_txt = val.substring(Field.selectionEnd, val.length);
Field.value += '(' + tag + ')';
}
</script>
what i want to do If the input value is "link: http: //example.com" I would like to change it and post it as "link: example.com".
Can you try in your url string :
var result = url.replace(/(\w+:|^)\/\//, '');
result variable will hold "link : example.com" in place of "link : http://example.com"
Use the replace() function to replace part of a string.
function formatText(tag) {
var Field = document.getElementById('entry_girdi');
Field.value = Field.value.replace("http://", "");
Field.value += '(' + tag + ')';
}
On keypress in a textarea, I need to select the id and separate it. How is this possible?
If I have some jQuery code:
$(document).on("keypress", "textarea", function(){$(this).
How can I get the textarea's id and separate it like if the id is id="ta1"
Assuming you are just interested in the numeric value part of the ID, you could easily strip off all non-numeric characters using a Regular Expression using the replace() function :
$('textarea').keypress(function(){
// Get your ID
var id = $(this).attr('id');
// Strip off any non-numeric values
var idNumber = id.replace(/\D+/g, '');
});
Working Example
$('textarea').keypress(function() {
// Get your ID
var id = $(this).attr('id');
// Strip off any non-numeric values
var idNumber = id.replace(/\D+/g, '');
alert('<textarea> #' + idNumber + ' was typed in');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>Text Area 1</pre>
<textarea id='ta1'></textarea>
<hr />
<pre>Text Area 2</pre>
<textarea id='ta2'></textarea>
<hr />
<pre>Text Area 3</pre>
<textarea id='ta3'></textarea>
try this
$('body').on('keypress','textarea',function(){
var id = $(this).attr('id')
var ta = id.substring(0,2);
var num = id.substring(2);
});
for some reason the gi modifier is behaving as case sensitive. Not sure what's going on, but maybe someone knows why this is. it works fine when the cases are the same. This JSFiddle will demonstrate my problem. Code below. Thanks.
javaScript:
var search_value = $('#search').val();
var search_regexp = new RegExp(search_value, "gi");
$('.searchable').each(function(){
var newText =(this).html().replace(search_value, "<span class = 'highlight'>" + search_value + "</span>");
$(this).html(newText);
});
HTML:
<input id = "search" value = "Red">
<div class = "searchable">this should be red</div>
<div class = "searchable">this should be Red</div>
Correct Code is
var search_value = $('#search').val();
var search_regexp = new RegExp(search_value, "gi");
$('.searchable').each(function(){
// var newText =$(this).html().replace(search_value, "<span class = 'highlight'>" + search_value + "</span>");
var newText =$(this).html().replace(search_regexp, function(matchRes) {
return "<span class = 'highlight'>" + matchRes + "</span>";
});
$(this).html(newText);
});
output
Fiddle
Issues with your code:-
First: search_regexp - You haven't used search_regexp anywhere in your code
Your Code
var newText =$(this).html().replace(search_value, "<span class = 'highlight'>" + search_value + "</span>");
Second
You are using search_value to replace. It will make both Red and red to either Red or red after replace.
eg: if search_value is Red then your output will be
this should be Red
this should be Red
you should use matched result instead of search_value
Third: How to use RegExp with replace function?
Correct Method is
var newText =$(this).html().replace(search_regexp, function(matchRes) {
return "<span class = 'highlight'>" + matchRes + "</span>";
});
Explanation
replace(<RegEx>, handler)
Your code isn't using your regex in the replace call, it's just using the search_value. This JSBin shows your code working: http://jsbin.com/toquz/1/
Do you actually want to replace the matches with the value (changing lowercase instances to uppercase in this example)? Using $.html() will also get you any markup within that element, so keep that in mind as well (in case there's a chance of having markup in the .searchable elements along with text.
Might be easier to do:
function highlight(term) {
var search_regexp = new RegExp(term, "gi");
$('.searchable').each(function(){
if (search_regexp.test($(this).html())) {
var highlighted = $(this).html().replace(search_regexp, function(m) {
return '<span class="highlight">'+m+'</span>';
});
$(this).html(highlighted);
}
});
}
Your original code in the JSBin is the highlightReplace() function.
I have this container:
<Div id="ListContainer">
I append these data to it :
' <a class="lesson" subjectID="'+sbj_ID+'"><b>
<span class="lesson_subject">' + sbj_Name + '</span></b></a> ';
I want to put the value of sbj_ID & sbj_Name in a variable.
localStorage['SubjectID']= "value of sbj_ID";
localStorage['SubjectName']="value of sbj_Name";
But I can't access them.
I tried :
$('#ListContainer').find('.lesson').attr('subjectID')
$('#ListContainer .lesson').children[0].getAttribute('SubjectID')
$('#ListContainer .lesson').children[2].innerHTML;
But they didn't work.
I don't know how you are appending the string, but the following is a working example:
var sbj_ID = 3;
var sbj_Name = "test";
var str = '<a class="lesson" subjectID="'+sbj_ID+'"><b><span class="lesson_subject">' + sbj_Name + '</span></b></a>';
$("#ListContainer").append(str);
alert($('#ListContainer').find('.lesson').attr('subjectID'));
-- See Demo --
I'm also not sure how LocalStorage is being used, but you should set the variables as the value, not the strings you have used:
localStorage['SubjectID']= sbj_ID;
localStorage['SubjectName']= sbj_Name;
Try this
localStorage['SubjectID']= $('.lesson').attr('subjectID');
localStorage['SubjectName']=$('.lesson_subject').text();
or
localStorage['SubjectName']=$('.lesson_subject').html();
This should work
localStorage['SubjectID'] = $('.lesson').attr('subjectID');
localStorage['SubjectName'] = $('.lesson_subject').text();
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.