java script function not invoking from html button click event - javascript

I am calling a js function in my spring boot - jsp war project, but the function is not getting invoked on the button click in IE browser.
<input type="button" id="btnEditUsrUpdate" value="Save" class="button" style="display: none;" onclick="updateUsers();">
But it is work if i am using jquery.
$('#btnEditUsrUpdate').click(function() {});
Note: Both cases are working in other browsers.
code:`
$(document).ready(function() {
checkUserRole();
if(('${makerCheckerFlag}'=='N')&& ('${groupSerialNumber}'!=1)){
document.getElementById("divUsrEditType").style.display = 'none';
}
var userRight=validateUserRight('${userAccessRights}',"2");
if(userRight==true){
document.getElementById("btnEditUsrUpdate").style.display="";
document.getElementById("btnEditUsrClear").style.display="";
}
var insCode=document.getElementById('hdnInstitutionCode').value;
if(insCode==0){
document.getElementById("formUsrEditProfile").disabled = "";
document.getElementById("formUsrEditGroup").disabled = "";
document.getElementById("divUsrEditRole").style.display = "none";
document.getElementById("spanUsrEditProfile").style.display = "none";
document.getElementById("divUsrEditType").style.display = "none";
document.getElementById("ddlEditUsrUserGroup").disabled = "disabled";
document.getElementById("ddlEditUsrUserProfile").disabled = "disabled";
}else{
document.getElementById("formUsrEditProfile").disabled = "true";
document.getElementById("formUsrEditGroup").disabled = "true";
}
$("form").bind("keyup", function (e) {
if (e.keyCode != 13) {
liveValidation();
} else{
updateUsers();
}
return false;
});
var userRole=document.getElementById("ddlEditUsrUserRole").value;
if(userRole=='Y'||userRole=='A'||userRole=='D'){
document.getElementById("divUsrEditType").style.display='none';
}else{
document.getElementById("divUsrEditType").style.display='table-row';
}
});`

Related

Dom Modification to clear Radio Button Info with Reset Button

So I have made a form that I can clear with a reset button. On this form, I have four radio buttons (that code is towards the top). When a button is selected, info comes up using "displayText".
<script type="text/javascript">
function textToDisplay (radioValue) {
console.log("textToDisplay + " + radioValue);
var displayText = "";
if (radioValue == "S") {
displayText = "Shortboards are under 7 ft in length.";
}
else if (radioValue == "L") {
displayText = "Longboards are usually between 8 and 10 ft.";
}
if (radioValue == "A") {
displayText = "Alternative boards defy easy aesthetic description.";
}
if (radioValue == "M") {
displayText = "Mid-Length surfboards are between 7 and 8 ft.";
}
return (displayText)
}
//DOM modification
function modifyDom(radioInput) {
console.log(radioInput.name + " + " + radioInput.value);
var displayText = textToDisplay(radioInput.value);
console.log(node);
var insertnode = document.getElementById("radioButtons");
var infonode = document.getElementById("info")
if (infonode === null) {
console.log("infonode does not yet exist");
var node = document.createElement("DIV");
node.setAttribute("id", "info");
node.className = "form-text infoText";
var textnode = document.createTextNode(displayText);
node.appendChild(textnode);
console.log(node);
insertnode.appendChild(node);
}
else {
console.log("infonode already exists");
infonode.innerHTML = displayText;
}
}
function checkboxesSelected (checkboxes, errorString) {
console.log("checkboxesSelected function");
var cbSelected = 0;
for (i=0; i<checkboxes.length; i++) {
if (checkboxes[i].checked) {
cbSelected += 1;
}
}
if (cbSelected < 2) {
return (errorString);
} else {
return "";
}
}
function validate (form) {
console.log("validate form");
var fail = "";
fail += checkboxesSelected(form.extras, "At least TWO fin setup needs
to be selected.\n")
if (fail == "") return true
else { alert(fail); return false }
}
</script>
When I reset my page using the button,
<input type="reset" name="reset" value="Reset">
the buttons themselves are cleared but the information that appeared from selecting the button is still visible. How can I reset the page so the displayText information is not visible? Thanks!
You can use an event listener for the reset event generated by clicking the reset button to execute cleanup code.
Here's a cut down example of the technique:
"use strict";
let myForm = document.getElementById("myForm");
let infoNode = document.getElementById("infonode");
let infoText = {
"S": "small board's are good",
"L": "large board's are good too"
};
myForm.addEventListener("change", function (event) {
if(event.target.name == "size") {
infoNode.innerHTML = infoText[ event.target.value];
}
}, false);
myForm.addEventListener("reset", function (event) {
infoNode.innerHTML = "";
}, false);
<form id="myForm">
<label> <input name="size" type="radio" value = "S"> Short</label><br>
<label> <input name="size" type="radio" value = "L"> Long</label><br>
<input type="reset" value="reset">
</form>
<div id="infonode"></div>
would suggest to remove the dynamically attached div#info:
document.getElementById("info").remove();
or blank it:
document.getElementById("info").innerHTML = "";

Validation with jquery and datalist

I have an similar problem like this post: Validation with datalist
However the answer of FelDev is in JavaScript I need it in jquery .
I am new to jquery therefore it would be of great help if some can help.
In the following the answer of FelDev:
let btn = document.getElementById("btnSend");
let form = document.getElementById("zeForm");
let input = document.getElementById("zeInput");
let msg = document.getElementById("msg");
let allowedValues = ["atown", "btown", "ctown"]; // same values as the options in your datalist
btn.onclick = function() {
let allGood = false;
allowedValues.forEach(function(elm) {
if(elm === input.value) {
allGood = true;
return;
}
})
if(allGood) {
msg.innerHTML = "Success!!";
msg.style.color = "green";
//form.submit();
} else {
msg.innerHTML = "This value is not accepted";
msg.style.color = "red";
}
msg.style.display = "inline";
}
#msg {
display: none;
}
#btnSend {
display: block;
margin-top:1rem;
}
<form id="zeForm">
<input id="zeInput" type="text" list="typ" name="name" placeholder="gtown" >
<datalist id="typ">
<!-- notice the absence of a <select> here... -->
<option value="atown">atown</option>
<option value="btown">btown</option>
<option value="ctown">ctown</option>
</datalist>
</input>
<p id="msg"></p>
<button id="btnSend" type="button">send</button>
</form>
So do you need a translation?
So the jquery of this js is :
let btn = $("#btnSend");
let form = $("#zeForm");
let input = $("#zeInput");
let msg = $("#msg");
let allowedValues = ["atown", "btown", "ctown"]; // same values as the options in your datalist
btn.on('click' , function() {
let allGood = false;
allowedValues.each(function(index, element) {
if (element === input.value) {
allGood = true;
return;
}
})
if (allGood) {
msg.text("Success!!");
msg.attr('style',"color:green");
//form.submit();
} else {
msg.text("This value is not accepted";
msg.attr('style',"color:red");
}
msg.attr('style',"display:inline");
});

how to make this javascript code shorter?

I created this small game while ago and now i'm trying it find a way to make it script short and simpler i was wondering if there is any another way . I have four pics of animals and blow the pic i have four names . what the game suppose to do is when the users click on right image and right name both the image and the name has to disappear. Any help really appreciate it
function image_select() {
var pic1 = document.getElementById("cow").value;
var text1 = document.getElementById("cow_t").value;
if (document.getElementById("cow").checked) {
if (pic1 == text1) {
var x = document.getElementById("cow_s");
x.play();
} else {
alert("wrong selection");
}
} else {
alert("no");
}
}
function image_select2() {
var pic2 = document.getElementById("dog").value;
var text2 = document.getElementById("dog_t").value;
if (document.getElementById("dog").checked) {
if (pic2 == text2) {
var x = document.getElementById("dog_s");
x.play();
} else {
alert("wrong selection");
}
} else {
alert("no");
}
}
function image_select3() {
var pic3 = document.getElementById("horse").value;
var text3 = document.getElementById("horse_t").value;
if (document.getElementById("horse").checked) {
if (pic3 == text3) {
var x = document.getElementById("horse_s");
x.play();
} else {
alert("wrong selection");
}
} else {
alert("no");
}
}
function image_select4() {
var pic4 = document.getElementById("pig").value;
var text4 = document.getElementById("pig_t").value;
if (document.getElementById("pig").checked) {
if (pic4 == text4) {
var x = document.getElementById("pig_s");
x.play();
} else {
alert("wrong selection");
}
} else {
alert("no");
}
}
<div style="margin-left:230px;">
<br>
<br>
<img onmousedown="dog.play()" height="142" width="142" src="image/cow.jpg" alt="Cow" data-value="cow" onclick="selectImage(event)" />
<img height="142" width="142" src="image/dog.jpg" alt="" data-value="dog" onclick="selectImage(event)"/>
<img height="142" width="142" src="image/horse.jpg" alt="" data-value="horse" onclick="selectImage(event)"/>
<img height="142" width="142" src="image/pig.jpg" data-value="pig" onclick="selectImage(event)"/>
</div>
<br>
<br>
<br>
<div style="margin-left:400px;">
<button type="button" onclick="selectName(event)" value="pig" id="pig_t">Cochon</button>
<button type="button" onclick="selectName(event)" value="cow" id="cow_t">Vache</button>
<button type="button" onclick="selectName(event)" value="horse" id="horse_t">Cheval</button>
<button type="button" onclick="selectName(event)" value="dog" id="dog_t">chien</button>
</div>
Just put the name of the animal to the function (in fact, you seem to already does so, since you pass the event to selectName, but I don't see this function in your code) :
function image_select_animal(prefix) {
var pic = document.getElementById(prefix).value;
var text = document.getElementById(prefix + "_t").value;
if (document.getElementById(prefix).checked) {
if (pic == text) {
var x = document.getElementById(prefix + "_s");
x.play();
} else {
alert("wrong selection");
}
} else {
alert("no");
}
}
and in onclick, call image_select_animal("pig") or equivalent for the other animals.
function image_select(name) {
var el = document.getElementById(name); // create this variable, as you use this element twice. Less DOM lookups.
var pic = el.value; // get the value of the `el` variable
var text = document.getElementById(name + '_t').value;
if (!el.checked) { // if not check
alert('no');
return; // return ends the function immediately, meaning the following lines are not read
}
if (pic != text) {
alert('wrong selection');
return; // return ends the function immediately, meaning the following lines are not read
}
document.getElementById(name + '_s').play(); // no need to store this in variable x, as you only use this element once.
}

Basic Javascript onclick

here's my code, brand new to coding trying to get the box "points" to return the sum of pointSum if "Ben" is typed into the box "winner". Just trying to work on some basics with this project. Attempting to make a bracket of sorts
<HTLML>
<head>
<script>
var pointSum = 0;
var firstRound = 20;
var secondRound = 50;
var thirdRound = 100;
var fourthRound = 150;
var fifthRound = 250;
var finalRound = 300;
var winnerOne = false;
var winnerTwo = false;
var winnerThree = false;
var winnerFour = false;
var winnerFive = false;
var winnerSix = false;
if (winnerOne = true){
pointSum+=firstRound
} else if (winnerTwo = true){
pointSum+=secondRound
} else if (winnerThree = true){
pointSum+=thirdRound
} else if (winnerFour = true){
pointSum+=fourthRound
} else if (winnerFive = true){
pointSum+=fifthRound
} else if (winnerSix = true){
pointSum+=finalRound
else
function tally() {if document.getElementById('winner') == "Ben" { winnerOne = true;
}
pointSum=document.getElementById("points").value;
}
</script>
</head>
<body>
<form>
Winner:
<input type="text" name="winner" id="winner" size="20">
Points:
<input type="text" name="points" id="points" size="20">
Submit
<button type= "button" onclick="tally()">Tally points</button>
</form>
</body>
</html>
UPDATE***** new code, getting better, not returning console errors but still not getting anything in the "points" box upon clicking tally
<HTLML>
<head>
<script>
var pointSum = 0;
var firstRound = 20;
var secondRound = 50;
var thirdRound = 100;
var fourthRound = 150;
var fifthRound = 250;
var finalRound = 300;
var winnerOne = false;
var winnerTwo = false;
var winnerThree = false;
var winnerFour = false;
var winnerFive = false;
var winnerSix = false;
function tally() {
var winner = document.getElementById("winner").value;
var firstWinner = "Ben";
if (winner == firstWinner){
winnerOne == true;
}
pointSum = document.getElementById("points").value;
}
if (winnerOne == true){
pointSum+=firstRound;
} else if (winnerTwo){
pointSum+=secondRound;
} else if (winnerThree){
pointSum+=thirdRound;
} else if (winnerFour){
pointSum+=fourthRound;
} else if (winnerFive){
pointSum+=fifthRound;
} else if (winnerSix){
pointSum+=finalRound;
}
</script>
</head>
<body>
<form>
Winner:
<input type="text" name="winner" id="winner" size="20">
Points:
<input type="text" name="points" id="points" size="20">
Submit
<button type= "button" onclick="tally()">Tally points</button>
</form>
<div class="updatePoints">
</div>
</body>
</html>
Your code has a few mistakes, lets change it a little bit!
First, you need to access 'value' atribbute of your winner element in your if statement, and surround all the statement in parenthesis
function tally() {
if (document.getElementById('winner').value == "Ben"){
winnerOne = true;
}
pointSum = document.getElementById("points").value;
}
Second, you use '==' to make comparison, you are using '=', it means that you are assign true to variables, and you're forgetting to put ';' at the end of lines! change this part:
if (winnerOne == true){
pointSum+=firstRound;
}
put all of your if/else like the example above!
Hint: when you are using if statement you can use like this:
if (winnerOne){ //you can omit == true, because if winnerOne is true, it will enter ind the if statement
//will enter here if winnerOne is true
}
if (!winnerOne){ //you can omit == false, because if winnerOne is not true, it will enter ind the if statement
//will enter here if winnerOne is false
}
You also have a left over else at the end of your if check which is invalid. You need to end the last else if statement with the };.
Are you trying to out put the text somewhere? I don't see any code that is handling this - you may want to add some HTML that will update like so:
<div class="updatePoints">
// leave empty
</div>
Then within your JavaScript you can always add some code to update the .updatePoints
var points = document.getElementByClass('updatePoints');
points.innerHTML = pointSum.value;
Have add some lines in your code and modify it with some comments. Can try at https://jsfiddle.net/8fhwg6ou/. Hope can help.
<HTLML>
<head>
<script>
var pointSum = 0;
var firstRound = 20;
var secondRound = 50;
var thirdRound = 100;
var fourthRound = 150;
var fifthRound = 250;
var finalRound = 300;
var winnerOne = false;
var winnerTwo = false;
var winnerThree = false;
var winnerFour = false;
var winnerFive = false;
var winnerSix = false;
function tally() {
var winner = document.getElementById("winner").value;
var firstWinner = "Ben";
if (winner == firstWinner){
winnerOne = true; // Use only one = symbol to assign value, not ==
pointSum = Number(document.getElementById("points").value); // moved from outside and convert to number
// This code will update point in Points box
document.getElementById("points").value = tally_pointsum(pointSum);
// The codes below will add the text in div, just remove the + sign if you don't like
document.getElementById("updatePoints").innerHTML += (tally_pointsum(pointSum) - pointSum) + " points added<br />";
}
}
// Wrap codes below become a function, lets call it tally_pointsum:
function tally_pointsum(pointSum) {
if (winnerOne == true){
pointSum+=firstRound;
} else if (winnerTwo){
pointSum+=secondRound;
} else if (winnerThree){
pointSum+=thirdRound;
} else if (winnerFour){
pointSum+=fourthRound;
} else if (winnerFive){
pointSum+=fifthRound;
} else if (winnerSix){
pointSum+=finalRound;
}
return pointSum; //return the sum to caller
}
</script>
</head>
<body>
<form>
Winner:
<input type="text" name="winner" id="winner" size="20">
Points:
<input type="text" name="points" id="points" size="20">
Submit
<button type= "button" onclick="tally()">Tally points</button>
</form>
<!-- change class="updatePoints" to id="updatePoints" for document.getElementById("updatePoints") -->
<div id="updatePoints">
</div>
Happy coding.

firefox iframe designmode not working

i have web application where users can type on iframe(rich-text-editor) when they click a button an iframe will show up.
html code when user click button for new iframe :
<input name="add_button" type="button" value="New frame" onClick="javascript:add_new_frame();"/>
javascript code for creating iframe and designmode
function add_new_frame(){
$("<iframe class=\"a\" id="a" name="a"/>").appendTo(id);
var editor = document.getElementById ("a");
editorDoc = editor.contentWindow.document;
editorDoc1 = document.getElementById ("a").contentDocument;
var editorBody = editorDoc.body;
if ('spellcheck' in editorBody) { // Firefox
editorBody.spellcheck = false;
}
if ('contentEditable' in editorBody) {
// allow contentEditable
editorBody.contentEditable = true;
editorDoc1.designMode = "on";
}
else {
if ('designMode' in editorDoc1) {
editorDoc1.designMode = "on";
}
}
}
I have tested above on (chrome,opera,safari,IE) and it's working fine. However, it's not working on FF, the iframe is showing up but i cannot edit it (designmode is not working).
is there any solution?
sorry for bad english
You missed \ in your iframe element to mask the "
function add_new_frame(){
$("<iframe class=\"a\" id=\"a\" name=\"a\"/>").appendTo(id);
var editor = document.getElementById ("a");
editorDoc = editor.contentWindow.document;
editorDoc1 = document.getElementById ("a").contentDocument;
var editorBody = editorDoc.body;
if ('spellcheck' in editorBody) { // Firefox
editorBody.spellcheck = false;
}
if ('contentEditable' in editorBody) {
// allow contentEditable
editorBody.contentEditable = true;
editorDoc1.designMode = "on";
}
else {
if ('designMode' in editorDoc1) {
editorDoc1.designMode = "on";
}
}
}
<html> <head> <title>Untitled Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$ = jQuery;
function frame() {
$("<iframe class=\"a\" id=\"a\" name=\"a\"/>").appendTo(id);
var editor = document.getElementById("a");
editorDoc = editor.contentWindow.document;
editorDoc1 = document.getElementById("a").contentDocument;
var editorBody = editorDoc.body;
if ('spellcheck' in editorBody) { // Firefox
editorBody.spellcheck = false;
}
if ('contentEditable' in editorBody) {
// allow contentEditable
editorBody.contentEditable = true;
editorDoc1.designMode = "on";
}
else {
if ('designMode' in editorDoc1) {
editorDoc1.designMode = "on";
}
}
}
</script>
</head>
<body>
<input name="add_button" type="button" value="New frame" onclick="frame()" />
<p id="id">
contents</p>
</body>
</html>

Categories