I have the codes below:
<script type="javascript">
var item = 001002004;
alert(item);
</script>
Whenever I see this page in view source in my web browser, I see these lines of code exactly as same as here. But when alert() function runs, I see an unknown message. It alrest '6295553'. I don't know where is this unknown value from. I'm sure anything don't happen to item and it did not change before of alert.
What do you think ? What's the problem ?
Any number prefixed with 0 will be considered as octal. and 0x for hexa decimal
I am guessing that you are having it as an numeric value like below or using parseInt function to parse the string and when you alert it you see a different value..
var item = 001002004;
alert(item);
Or probably you are doing something like below
var item = '001002004';
alert(parseInt(item));
See more details on parseInt #MDN
DEMO HERE
Try changing the script type to "text/javascript" <script type="text/javascript">, or don't use the type attribute at all. See this jsfiddle
http://jsfiddle.net/3jK2v/
Please have a look at above fiddle. It does not alert anything anomalous for me.
are you sure you are not changing the variable -- maybe with a cap. eg
var item = 'some value';
alert(Item);
Related
I got a variable Javascrpit which has a number as a string in this case 0.84. I'm trying to convert it into a float but when I try to it appears a 0 as float instead the 0.84.
I'm using this:
var pot="0.84";
var asd = parseFloat(pot);
console.log(asd);
EDIT:
This is not exactly the example. I recover data from the HTML and it works for other numbers but not for this. It is difficult to explain my problem exactly. It is a lot of code and works for other numbers so don't know exactly.
Your input is not "0.84". If you test with that, you will get the correct answer. Your input has something else inside, like spaces, for example:
"0 .84"
This should be the solution:
parseFloat(pod.replace(/ /g, ""))
I have tried this example on my end and it completely worked. However, you can try to instead input the string value directly into the parse float() function and it should print our your expected value. If you still want to assign the parsefloat() to a variable, then try to either rewrite the code or re-open your IDE because the code should work.
var pot = "0.84"
console.log(parseFloat(pot))
or you can just write it in one line
console.log(parseFloat("0.84"))
I've got several input fields and such jquery code:
jQuery(document).ready(function($){
$('input').change(function(){
var amount1=$('.product-addon-wesprzyj-autora input').val();
var amount2=$('#pa_kategoria-cenowa').val();
var amount3=$('.quantity .qty').val();
var fractal=0.01;
var Total=(amount1*amount2*fractal*amount3)+(' PLN');
$('.product-addon-kwota-dla-autora input').attr('value', Total);
});
});
Strange thing is that jquery code works. In real time in changes values in my inputs, BUT after clicking sumbit, value from '.product-addon-kwota-dla-autora input' seems to be empty in database. Why is that? is there any way of pushinig this code to work? If i will type via keyboard data into that field, everything works. ANy ideas what is here wrong?
With the code you provided, I am not quite sure what your problem is. It could be a problem in your server side code where you save the data to the table. You need to debug and that find out.
But make sure that you are setting the form values properly so that your server side code will have the correct data from the form. You may consider using the val() method to set value.
$('.product-addon-kwota-dla-autora input').val(Total);
Also , when you read values from form inputs and use it for numeric operations, Consider converting the type to a numeric version by using parseFloat() or parseInt()
var Total=parseFloat(amount1)*parseFloat(amount2)*parseFloat(fractal)
*parseFloat(amount3)+(' PLN');
Also, You may inspect your browser's network tab to see what data your browser is posting to the server code. That should help you to understand where your problem is.
it might me that your "Total" variable is empty. alert('Total') and check for result. if it shows some value then try to change your line from this
$('.product-addon-kwota-dla-autora input').attr('value', Total);
into this
$('.product-addon-kwota-dla-autora input').val(Total);
Or how would I convert a list of SelectItems to a JavaScript array?
Currently I am trying this:
<h:outputScript>
<!-- Trailing commas valid as per http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5 -->
var possibleOption = [<ui:repeat value="#{bean.mySelectItems}" var="selectItem">"#{selectItem.value}",</ui:repeat>];
var firstOption = possibleOption[0];
</h:outputScript>
And it works, except that firstOption is undefined although possibleOption gets correctly populated when I check in the console. Maybe a timing problem? Is this even valid JSF, and if so, is there a "blocking" version of ui:repeat or something?
Or which other approach would you recommend?
Aaaah, I got it:
actually I was using:
var chosenOption = '#{empty bean.chosenOption ? possibleOption[0] : bean.chosenOption}';
Which is (now) of course wrong, because I was using possibleOption[0] inside the EL expression headbang
Sorry, one should always post the actual code I guess, not some dumbed down showcase ;)
This seems so simple and I honestly can't see why this isn't working. Looked at other answers on here and mine still isn't working.
var randomArray = new Array();
randomArray[0] = 859;
alert(randomArray[0]);
How come nothing happens? It's probably an easy answer but I can't work it out.
Also, I'm trying to work out how to put a random number in the array so rather than putting 859 in index 0 lets say I want to put a random number between 1 and 20 in, so far I've got this but thats not working either
randomArray[1]=Math.floor(Math.random()*3);
EDIT: the .toString on alert seemed to fix it, thanks guys, knew it would be something small!
Is your javascript being referenced properly in a script tag with the correct type set?
<script type="text/javascript" ...> </script>
If not, it's fully possible your browser is simply ignoring it.
This is a wild guess, because your question doesn't contain enough information to know for sure, but make sure that your code is in a <script> block that looks like this:
<script>
// your code
</script>
Get rid of any "type" or "language" attribute on the tag; they're not needed and they're a source of error. If the "type" value is mispelled, then the browser will completely ignore the code.
Try calling .ToString() on your array property.
alert(randomArray[0].toString());
I'm having an issue with javascript whereby i am performing the following to close a popup window and update a field in the parent window with the required value. Code looks something like this:
<script language="javascript" type="text/javascript">
var FieldID = document.form.field22-1.value;
self.parent.opener.document.+FieldID = 'some text';
window.top.window.close();
</script>
However I am getting the following error:
Error: missing ; before statement
I have a funny feeling the javascript is interpreting the field id (field22-1) as having a subtraction in it. Which I guess would make sense. Any ideas/help would be ridiculously appreciated, really don't want to have to go back in and change the - in the code!
Thanks in advance!
Use document.getElementById('field22-1').value instead.
You might also need to fix this:
self.parent.opener.document[FieldID] = 'some text';
In JavaScript, any property of any object can be accessed either via dot notation, e.g. foo.bar, or bracket notation, e.g. foo["bar"]. The latter is necessary when your property is not a legal identifier (as in your case):
var FieldID = document.form["field22-1"].value;
Alternatively, if this is an actual id attribute, you should use:
var FieldID = document.getElementById('field22-1').value;
You could also use document.form['field22-1'].value.
You can use document.getElementById('field22-1').value