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;
Related
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 want to use the function data as id, tried alot but nothing worked, how should i do it here is my code, thanks in advance
function SLIDE_HIDE(SLIDEID)
{
$("#($SLIDEID).val()").prop('value', 'hide');
}
function SLIDE_HIDE(slideId)
{
$("#" + $(slideId).val()).prop('value', 'hide');
}
You need to concatenate:
$("#" + SLIDEID)
Which means that if the SLIDEID argument is, say, the string "test" then it will be like saying $("#test").
You were trying to select an element with an id equal to all of the text in the string after #, i.e., an element with id="($SLIDEID).val()".
If you are trying to hide the element that has the id in the SLIDEID argument then do this:
$("#" + SLIDEID).hide();
If you want to change that element's value then (assuming it is a form element like an input):
$("#" + SLIDEID).val('hide');
The code you showed sort of looks like you're trying to retrieve the value from an element with the id specified in SLIDEID and then use that value as the id to look up another element and set that other element's value to the string 'hide'. That doesn't really make sense to me, but if it's what you want to do:
$("#" + $("#" + SLIDEID).val()).prop('value', 'hide');
I have the code to dynamically generate the textboxes. I want to multiply the values of quantity and rate textboxes and display the result in total textbox and post it to next page.
The fiddle http://jsfiddle.net/hEByw/ that shows how textboxes are dynamically generated.
I have tried the following part of code to multiply the two textbox values but its not working for me. please see the fiddle for complete code.
//To multiply two textbox values
$('#qty + counter + ').keyup(calculate);
$('#rates + counter + ').keyup(calculate);
function calculate(e)
{
$('#total + counter + ').val($('#qty + counter +').val() * $('#rates + counter+').val());
}
Can any one suggest where am I going wrong or the correct way of doing it. I am new to jquery. Any help is appreciated.Thanks in advance.
I think the problem is here
$('#rates + counter + ')
It should be made to
$('#rates' + counter)
EDIT :
I analysed it with jsfiddle but the problem is with your logic. How can you get the counter when you are pressing a key. Counter will be used and it will be undefined once the controls are added.
Edit 2 :
At last I came up with the answer.
I had to tweak around a bit but that i hope it will satisfy you
Check This JsFiddle Link
Sorry for editing your own link
I just added a title attribute and i used it instead of the counter variable.
e.target.title For the counter
title = '+ counter + ' in your HTml
Refer the link. Hope this helps you
As long as you are using jquery 1.4+:
$('#qty' + counter).live('keyup', function(){ calculate(counter); });
$('#rates' + counter).live('keyup', function(){ calculate(counter); });
function calculate(counter)
{
total = Number($('#qty' + counter).val())*Number($('#rates' + counter).val())'
$('#total' + counter).val(total);
}
You never know the value of counter when the event triggers.
$('#qty' + counter).on("blur", function() {
calculate($(this));
});
$('#rates' + counter).on("blur", function() {
calculate($(this));
});
function calculate(el) {
var counter = el.attr("id").indexOf("qty") != -1 ? el.attr("id").substring(3) : el.attr("id").substring(5);
var qty = isNaN(parseInt($('#qty' + counter).val())) ? 0 : parseInt($('#qty' + counter).val());
var rate = isNaN(parseInt($('#rates' + counter).val())) ? 0 : parseInt($('#rates' + counter).val());
$("#total" + counter).val(qty * rate);
}
All,
I'm working with this Ajax tutorial which basically pull some records from a database and display them. I want to modify the code with a button that will show the records one by one instead of together. The behavior I want to achieve is to get the next record each time the user clicks the Show next button.
To do that I have built a little counter in the Ajax function that is used as an index to decide which array element to print. This doesn't work. **My question is: why doesn't my counter work?
Here is the html and Ajax code:
<html>
<body>
<script type="text/javascript">
<!--
function createRequest() {
//Returns HttpRequest
return request;
}
//My attempt at a counter. This doesn't work.
var index=null;
function calcIndex(){
if(index==null){
index=0;
}else{
index += index;
}
return index;
}
(.....)
</body>
</html>
Your calcIndex function declaration is broken, it's missing the function part. And are you sure you want to set index += index? That would be sort of odd. Not only that, even if you fix it and leave it as-is, index will never increment past zero:
var index=null;
function calcIndex(){
if(index==null){
index=0; // First call, now index = 0
}else{
index += index; // Second, third, ... nth call: index = 0 + 0
}
return index;
}
Let's simplify:
var index = 0;
function calcIndex(){
return index++; // Returns zero first, then 1, then 2...
}
But wait, at that point why do you need a function at all? Instead you can simply do:
var index = 0;
...
//index = calcIndex(); No point here
var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex + "&index=" + index++;
^^^^^^^^
Cheers
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");
});