Javascript to jquery issue - javascript

I made a pure JavaScript file the past days. It's a fully operational BlackJack game. However i was told to try and use as much jquery as possible on my existing file. So I can understand the basics of jQuery.
Things like
$("#Result").empty();
and
$("#hit").attr("disabled", "disabled");
$("#hit").removeAttr("disabled");
all work fine. But i have a problem with this one for example:
function player_blackjack() {
$("#Result").text("BlackJack!");
var bet = parseInt($("#bet").val());
var bank = parseInt(document.getElementById("Bank").innerHTML);
bank = bank + (bet * 2.5);
document.getElementById("Bank").innerHTML = bank;
start_game_buttons();
}
The first lines work, all except the var bank and the line above start_game_buttons();, thats why they are still javascript written.
If i change the var bank line to this one:
var bank = parseInt($("#Bank").html);
It just doesn't work. When i run this and make myself get a blackjack ill get a NaN where the bank should be. I also tried:
var bank = parseInt($("#Bank").val());
Im basically just trying whatever i can find, but since i can't figure it out im trying my luck here.
Thanks in advance!

You forgot the () after the html method, it should be:
var bank = parseInt($("#Bank").html());
Although, why are you using parseInt on an html() call? If this is an input -- use .val()

Related

Is there a JavaScript alternative to PIL?

I'm trying to convert some old python code to JS and I'm having trouble finding an alternative to it. These are some examples of the python code I'm trying to convert.
image_array = np.ones([16, 16, 4],np.uint8)*255
im = Image.fromarray(image_array)
im.save(name)
----------------------------
image = Image.open(new_image_filename)
impt = np.array(image)
Sorry if this doesnt make sense - thanks for taking the time to look at it

how can i create a reg exp or array to change this?

ok guys i have been working on these, this is like my main project and im pretty anxious about it, i have been practicing but im still a newbie. My code is like this and it does what it has to do, but im thinking that it could be improved to be better and more reusable, sorry to spam if i am, i already asked on the spanish version of the website with no satisfactory answer, im new to web developing and to this site, i always read the content on this site to answer my questions but for this time i didnt know how to exactly use the previous answers to fix my code, since im new to web developing and im trying to use jquery bit by bit. As i said my question is how can i create an array or a reg exp that does all the things this code does? without having to use .replace function all those times
i have tried urlencode function, and tried to iterate over arrays on jquery but i still dont know how to do it properly.
$( ".linkbestia" ).each(function() {
lnk = $(this).text();
enlace= $(this).attr("href");
espacios=lnk.replace(" ","_");
maslimpio=espacios.replace("'","%27");
muchomaslimpio=maslimpio.replace("(","%28");
muchomuchomaslimpio=maslimpio.replace(")","%29");
nuevoenlace=$(this).attr("href",enlace+muchomuchomaslimpio);
});
the actual output is for example codedquote'replaced space as i said it already does what it has to do, but i know it can be improved, i hope you guys help me since in my country these kind of questions cant be answered without a ton of difficulties
what it does right now:
what the user writes would look like this
the result would look like this
If I understand correctly you want to take href from below:
<a class="linknpc" href="url/in/url/url/The%27White_Mob">
and expected output is
The'White_Mob
after you get the href and lets say the var enlace looks like below.
var enlace = "url/in/url/url/The%27White_Mob"
Below will first use String split on '/' to get all sections from href and from resultant array get the last element by pop() and use decodeURIComponent to decode the encoded uri.
var ans = decodeURIComponent(enlace.split('/').pop())
ans would now have the value: The'White_Mob
note: If the href ends with '/' then you need to adjust above solution accordingly

JS variable inside of PHP inside of JS inside of PHP [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
Having a bad day. This one has stumped me all morning. All the solutions I've found have stopped one step short of where I need to go.
I have a legacy PHP/JS app that I'm working on. Rather than trying to explain it, I'll just show what I need to do.
<?php
$phpDate_1 = new Date($someDate);
$phpDate_2 = new Date($someOtherDate);
//...There are a bunch of these
$phpDate_n = new Date($endOfTime);
<script language="javascript">
function myFunction() {
var line = aUserSelection; //an int from user which tells me what date to use
//Next line is the problem. I'm trying to pull the month from the appropriate PHP date into the JS variable.
var theMonth = "<?php echo $phpDate_" + line + "->getMonth();?>";
}
</script>
?>
I must have tried 20-30 combinations of single and double quotes, escapes, dots, pluses, and so on, but I keep getting errors over the "line" part. Unexpected character, encapsed strings, etc.
Hoping someone can point me in the right direction because my brain is fried at this point. Answers in pure JS and PHP only please because that's how the app is built. Thanks.
You need to close your php (?>) before outputting the javascript to fix the syntax error that you got.
However, with that said, you are trying to incorporate the javascript line variable into the variable name for $phpDate, to generate something like $phpDate_1.
If you don't want to go with an AJAX solution, your best bet would be to output each line's date into a javascript array. This is strongly discouraged, but if this is a legacy application that you cannot make many changes to, this might be your only option.

Java Script and RegEx

In application that I'am working on I have dynamically generated UI it is number of controls and listed entries depends on records from DB.What I'am trying to achieve is to write RegEx that would help me extract some numbers assigned as part of controls ID.
The way how I'am assigning IDs is : cText+(row that it represents in its group)+G+(group number).For example cText4G12
What I'am looking for is to get from that ID what Row it represents in example is 4 and it's group 12. I've never used RegEx before so I'am asking for your help :)
RegEx is not something that is easy to get your head around, so I would strongly recommend that you spend a bit of time to learn it - as it will be beneficial to you in the long run.
However, against my best judgement - here is code that will get you what you need..
var id = "cText4G12";
var result = id.match(/^cText(\d+)G(\d+)$/);
result[1] will contain "4"
result[2] will contain "12"
You don't really need regex. You can simply do:
var my_id = "cText4G12";
var row = my_id.slice(my_id.indexOf("cText")+5,my_id.indexOf("G"));
var group = my_id.slice(my_id.indexOf("G")+1);

Greasemonkey, replace every occurence of string every second

i try to figure out a greasemonkey script that replaces every onmousedown on a site with an ondblclick. And i want it to constantly update, like every 1,5 Seconds, because the page refreshes using AJAX.
This is the script i came up with, but it doesn't seem to be working.
window.setInterval(document.body.innerHTML= document.body.innerHTML.replace('onmousedown','ondblclick');,1500);
The page it should work with is internal use only. But a good example would be the google search, where onmousedown is used for the links of the results to swap out the URL before you click it.
I also tried it without the semicolon after the document.body.innerHTML.replace.
I'm really new to JavaScript, but since i'm the only one in the company who can code, this one is stuck with me.
Any help would be appreciated.
Also, a small "side question"
Do i have to use #exclude, or is it enough to only use #include internal.companysite.tld* so it will only work on this site ?
A direct answer: you need to supply a function to setInterval - and it's best to set a variable so that you can later cancel it with clearInterval() if necessary.
function myF(){document.body....;}
var myIntv = setInterval(myF, 1500);
You could also do it using an anonymous function in one line as you're trying to do... do that this way:
var myIntv = setInterval(function(){document.body....;}, 1500);
I wouldn't suggest this as the solution to your problem. What it sounds like you want to do is manipulate the active DOM - not really change the UI. You likely need something like this:
var objs = document.getElementsBy__(); // ById/ByName/etc - depends on which ones you want
for (var i in objs){objs[i].ondblclick = objs[i].onmousedown;objs[i].onmousedown = undefined;} // just an example - but this should convey the basic idea
Even better, if you can use jQuery, then you'll be able to select the proper nodes more easily and manipulate the event handlers in a more manageable way:
$(".class.for.example").each(function(){this.ondblclick = this.onmousedown;this.onmousedown = undefined;}); // just an example - there are multiple ways to set and clear these

Categories