This question already has answers here:
What's the purpose (if any) of "javascript:" in event handler tags?
(10 answers)
Closed 7 years ago.
Recently I have seen the following HTML when viewing source code in Chrome:
<select class="menu_combo" onchange="javascript:myfunction()">
<!-- rest of code goes here -->
Why is the word javascript and a colon (:) added in front of myfunction()? I thought that onchange was a javascript event. If so, what is the need for this?
There is no need for it; it’s a mistake/misconception by the author. It still works because it’s a valid label, used properly with loops:
loop: while (true) {
while (true) {
break loop;
}
}
Related
This question already has answers here:
remove readonly from input
(2 answers)
Closed 2 years ago.
I find myself at a loss. I am charged with the seemingly trivial task of making a text field (not) read only, if some condition is true. well, I have hit the wall.
I have tried various versions of the following:
if(someTextBox.enable==false){
someTextBox.enable=true;}
including
someTextBox.setAttribute("enable", true);
and even
someTextBox.setAttribute("readOnly", true);
I am using Chrome devtools, and can see various incarnations of null, including ignored if-clauses.
You can simply do:
document.getElementById("myText").readOnly = false;
This question already has answers here:
Remove first character from a string if it is a comma
(4 answers)
Closed 3 years ago.
My website uses product references in this style: "R202020"
I want them to be shown like this for the users of my website: "BA2202020"
So basically I'm looking for a script, which formats the style of my reference numbers (should affect a ".reference" class I've created) by:
Removing the "R" in the original reference - replacing it with a "BA2" in stead - leaving the rest as it is (the "202020" part).
How can I do this?
Find 1st character of your string using string[0] and replace that with your desire value like below.
var string=$('.YourClass').text();
var result = string.replace(string[0],'BA2');
$('.YourClass').text(result);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class='YourClass'>R202020</span>
Try replace method. https://www.w3schools.com/jsref/jsref_replace.asp
'R202020'.replace('R2','BA2') // BA202020
This question already has answers here:
What is the point of using labels in javascript (label: stuff here) outside switches?
(2 answers)
Closed 5 years ago.
I was reading a book and I am not able to find how this search: is working.
search:while (true){
n++;
for(let i=2;i<=Math.sqrt(n);i++){
if(n%i===0){
continue search;
}
}
Is it predefined in javascript, or what is it?
It's a label, and continue tells the loop to go back to the label.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue#Using_continue_with_a_label
This question already has an answer here:
Does HTML comment <!-- act as a single-line comment in JavaScript, and why?
(1 answer)
Closed 7 years ago.
I have taken on a project that someone else has worked on and I am running into a bunch of script blocks formatted like this:
<script>
<!--
$('...').live('change', function() {
if (...) {
$('...').hide();
$('...').show();
} else {
$('...').show();
$('...').hide();
}
});
//-->
</script>
They are not even the correct comment format for JavaScript.
The code still works but should I worry that this might break in the long term if I start adding to it?
Is this just blatantly wrong and should I remove all the invalid comments?
To formalize my comment.
Back in the day not all browsers supported JavaScript. To make the JS disappear it would be wrapped in an HTML comment and the browser would effectively ignore it.
This hasn't been relevant for some time now and it can be safely deleted.
It can also be left in without harm, but ew.
By way of example, this question from 2009 asks the question re: relevancy, not what it actually is.
This also means I'm old :(
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
$(‘<element>’) vs $(‘<element />’) in jQuery
I am used to write $('<div>').
But today I saw a presentation about advanced-jquery by john-resig who use the following syntax $('<div/>').
http://loft.bocoup.com/john-resig-advanced-jquery/
To me they seem to produce the same output.
My question is: is there some difference between
$('<div>') and $('<div/>')?
No, jQuery will normalize those statements into the exact same.
In some earlier version of jQuery tho, it happend to be that <div> was actually faster than <div/> for whatever reason. I don't know yet, if that still applies.
http://jsperf.com/jquery-constructor-performance
Seems like this bug/feature is no longer true.
<div>, <div/>, <div></div>, and even <div/></div> (Yes, this one will only create one element) all trigger the singleTag regular rexpression which makes jQuery to call document.createElement("div"). It was never passed to any html parser at all.
Here's the regex that you can play with, if it returns true, it will be document.createElement'd
var rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
<div> is an opening tag. <div/> is a self-closing tag. In this context, however, there is no difference.