This is the function that I have
function set(sub) {
var input = prompt("Insert your desired " + sub + " information, leave blank if you use an automated system to generate this information.");
$('#' + sub).html(input).show();
var textarea = $('textarea');
textarea.html(textarea.html().replace(sub+"=",sub+"="+input));
}
Usually the sub is s1, or s2, or s3. Now the issue is that I want the replaced value to reset if the input is blank.
So lets say I input that I want to set s1 to equal a, so in the text area it will replace s1= with, s1=a, now if the input is empty I want the s1=a to revert back to s1=
How about this? It uses a regular expression (Regex) to do the replacements.
function set(sub) {
var input = prompt("Insert your desired " + sub + " information, leave blank if you use an automated system to generate this information.");
$('#' + sub).html(input).show();
var pattern = new RegExp('(' + sub + '=[^&]*)', 'ig');
var $textarea = $('textarea');
// Do Replacements
var content = $textarea.html().replace(pattern, sub + '=' + input);
$textarea.html(content);
}ā
You can replace the html with something like:
$(selector).html(function(i, orig) {
return orig.replace("1234", "5678");
});
Related
I'm pretty new to web programming so, making a logic for a web-page, I made a mistake and now I'm in trouble.
I named some divs like "Dv_1.2.1.3" (without knowing the problems linked to using the dot) and I have issues trying to clone (via jquery called by button) some of these.
The button id contains the id of the div I want to clone, so, my logic is:
1) extract the id of the div;
2) get the div and clone it (giving a new id).
I'm stuck with getting the div because of the dots in the id.
The below code is what I've done so far:
$('.CloneDiv').click(function () {
var SplittedId = (this.id).split('_');
if (SplittedId[0]=='Clone'){
alert('SplittedId 1 =' + SplittedId[1]);
//Modify id to use it to find the div to clone
var UsableId = SplittedId[1].replace(/\./g, '\\\\.');
alert('UsableId =' + UsableId);
//Count existing elements
var ClonedNum = $('#' + 'Dv_' + UsableId + '_').length;
ClonedNum++;
var OrigElem = $('#' + 'Dv_' + UsableId).length;
alert('OrigElem =' + OrigElem); //THIS IS 0
//NO ELEMENTS FOUND BUT THE ELEMENT EXISTS
//Clone the element and give new id
var ClonedElem = $('#' + 'Dv_' + UsableId).clone().attr('id', function( i, val ) {
return val + '_' + ClonedNum;
});
ClonedElem.find("input").val("");
if (ClonedNum > 1){
ClonedNum--;
var AnteId = '#' + 'Dv_' + UsableId + '_' + ClonedNum;
alert(AnteId);
$(AnteId).after(ClonedElem);
}else{
var AnteId = '#' + 'Dv_' + UsableId;
alert('AnteId = ' + AnteId);
$(AnteId).after(ClonedElem);
};
}else if(SplittedId[0]=='Del'){
alert(SplittedId[0]);
alert('Del');
}else{
//error
};
});
Might these help: developer.mozilla.org/en-US/docs/Web/API/CSS/escape , Polyfill: github.com/mathiasbynens/CSS.escape/blob/master/css.escape.jāās
I want to add the result value plus already exists value in that textbox. but addition is not working concatenation is working
$.post('includes/ajax_timesheet.php', {
'action': 'add_kitamount',
'jobnumber': jobno,
'invoiceno': inv_no
}, function (data) {
var tot1 = $('#tot_dayrate').val();
var tot2 = $.trim(data);
var tot = tot1 + tot2;
alert(tot);
$("#tot_dayrate").val(tot);
});
Concatenation is happening because the values are being treated as string by + operator . Parse the values to number using any of the availaible javascript functions and then you will get correct total.
Ofcourse you need to handle for invalid inputs . Below is only showing an example for parse to number function.
var tot = parseInt(tot1) + parseInt(tot2);
Check here for string to number conversion and good explanation of difference between Number() and parseInt() , parseFloat() functions.
var tot = parseFloat(tot1) + parseFloat(tot2);
Convert to number
var tot = Number(tot1) + Number(tot2);
Or
var tot = parseInt(tot1) + tot2;
.val() returns the value of the element in String. You will first need to convert to to Number for performing arithmetic operations.
You can use Number() to convert the string into numbered format.
So your code would look something like this,
var tot1 = $('#tot_dayrate').val();
if(tot1!='') {
tot1=Number(tot1);
}
var tot2 = $.trim(data);
if(tot2!='') {
tot2=Number(tot2);
}
var tot = tot1 + tot2;
Make sure to check for blank value before converting String into int.
I have a problem with jQuery to obtain each value from jSon string and modify a div or span or other id with value obtained from a jSon string.
At the start of each PHP file i have an SQL request generate an hidden input with a jSon string as value. This is for multilanguage for example in english the generated string is
<input type="hidden" id="page_json_language_home" value='{
"label_title":"My WebSite",
"label_settings":"Settings",
"label_subscription":"Subscription"
}' />
for french :
<input type="hidden" id="page_json_language_home" value='{
"label_title":"Mon site web",
"label_settings":"Parametres",
"label_subscription":"Abonnement"
}' />
this is work fine !
After that i have a javascript using jquery to match each label_xxx with value
i have many html code like this
<title id="label_title></title>
<div id="label_settings"></div>
or
<span id="label_subscription"></span>
This is my (partial) code in my javascript file i called to obtain the json string from hidden input :
var _getPageJsonLanguage = function(id) {
if (!id)
id = "page_json_language";
else
id = "page_json_language_" + id;
var json = $("#" + id).val();
var data = bsc.data.jsonParse(json);
return data;
};
This is work fine too !
The code in problem is :
data_language = bsc.page.getPageJsonLanguage("home");
var j = 0;
var language = [];
for (i in data_language) {
console.log("i in language = " + i);
language[j] = i;
console.log("language[j] = " + language[j]);
$("#" + i).html(language[j]);
j++;
}
The result can i obtain in browser 1) undefined for each label or 2) label_xxx for each label_xxx
I need help to access each value of each label_xxx .
I can't obtain the value, this is my last try....
I believe the problem is in your for in loop, you never actually grab the value, only the key:
for (i in data_language) {
console.log("i in language = " + i);
language[j] = data_language[i]; //changed this line to actually grab the value
console.log("language[j] = " + language[j]);
$("#" + i).html(language[j]);
j++;
}
If you are receiving undefined, it may be due to the JSON not being parsed correctly. Since your using jQuery, you can always run $.parseJSON(json) to be sure.
Fiddle accessing your JSON in a for in loop and logging: http://jsfiddle.net/tymeJV/CKBLc/1/
I hope this will work -
var data_language = JSON.parse($("#page_json_language_home").val());
var language = [];
var j = 0;
for (i in data_language) {
console.log("i in language = " + i);
language[j] = data_language[i];
console.log("language[j] = " + language[j]);
$("#" + i).html(language[j]);
j++;
}
I have a link, and every time it is clicked i need to add a random id to the url, these id's can't be the same, and ideally need to be saved somewhere.
I'm working in php, which i'm pretty new to, therefore I'm not sure how to go about this.
you can do something like that
link
and by using jquery
<script>
var alreadyUsed = ""
function AppendIDToURL(){
var id ;
var loop = true;
while(loop){
id = 1 + Math.floor(Math.random() * 100);
if (alreadyUsed.indexOf("," + id + ",") == -1){
alreadyUsed += "," + id + ",";
loop = false;
}
}
var url = "temp.php/" + id;
//use ajax or any other thing to save this id.
}
</script>
Hi i am new to ajax and trying to add two numbers in ajax function here is the code:
$("#next_btn").click(function(){
Display_Load();
var page = this.title;
var subtract = 1;
$("#content").load("pagination_brand.php?page=" + page, Hide_Load());
this.title = parseInt(page + 1);
});
in this function i am calling the div's title value and on click i want to add 1 value in to that number just like if title is having 1 so onclick it will become 2 but here its taking as string add when i see the output it disply 11 apart of 2.
It must be:
this.title = parseInt(page) + 1;
you need to do it like
for integers
parseInt(number1,10) + parseInt(1,10)
for floats/decimals
parseFloat(number1) + parseFloat(1,10)
Just parse the number then it will treat it like integer rather than string
this.title = parseInt(page)+1;