First off, I'm sure there's some obvious answer to this, but I'll ask still.
So I have this project where the user is rolling dice, and writing what the dice is for and what is being done.
It works through html forms and the javascript prints the results without refreshing the page. I got all that working.
My current issue is that I want the result of the code to move on to the next line, and continue on when you click 'submit' again.
$( "#modTool" ).submit(function( event ) {
event.preventDefault();
console.log($(this).serialize());
var a = document.getElementById("modTool").elements.namedItem("user").value;
var b = document.getElementById("modTool").elements.namedItem("actionTaken").value;
var c = document.getElementById("modTool").elements.namedItem("target").value;
var amount = document.getElementById("modTool").elements.namedItem("amountOfDice").value;
var sides = document.getElementById("modTool").elements.namedItem("typeOfDice").value;
var d = document.getElementById("modTool").elements.namedItem("bonus").value;
var diceRoll = rollDice(amount, sides);
var display=document.getElementById("Prompt");
display.innerHTML = "> " + a + " uses " + b + " towards " + c + "<br />" + "> " + diceRoll + " Bonus " + d;
});
https://i.gyazo.com/fe3ffa835ed1acf41c08c7f6fe46ea93.png
It needs to both go to the next line, when I click 'submit' again, as well as having a scrollbar.
Any ideas?
Use this line:
display.innerHTML += "> " + a + " uses " + b + " towards " + c + "<br />" + "> " + diceRoll + " Bonus " + d + "<br /><br />";
Instead of:
display.innerHTML = "> " + a + " uses " + b + " towards " + c + "<br />" + "> " + diceRoll + " Bonus " + d;
display.scrollTop = display.scrollHeight;
Add overflow to your #Prompt like this:
#Prompt {
border-style: groove;
height: 400px;
max-width:98%;
width:auto;
position:relative;
overflow-y: auto;
}
See working example:
function rollDie(sides) {
if(!sides) sides = 6;
with(Math) return 1 + floor(random() * sides);
}
function rollDice(number, sides) {
var total = 0;
while(number-- > 0) total += rollDie(sides);
return total;
}
$( "#modTool" ).submit(function( event ) {
event.preventDefault();
console.log($(this).serialize());
var a = document.getElementById("modTool").elements.namedItem("user").value;
var b = document.getElementById("modTool").elements.namedItem("actionTaken").value;
var c = document.getElementById("modTool").elements.namedItem("target").value;
var amount = document.getElementById("modTool").elements.namedItem("amountOfDice").value;
var sides = document.getElementById("modTool").elements.namedItem("typeOfDice").value;
var d = document.getElementById("modTool").elements.namedItem("bonus").value;
var diceRoll = rollDice(amount, sides);
var display=document.getElementById("Prompt");
display.innerHTML += "> " + a + " uses " + b + " towards " + c + "<br />" + "> " + diceRoll + " Bonus " + d + "<br /><br />";
display.scrollTop = display.scrollHeight;
});
* {
font-family: Helvetica, Arial, sans-serif;
}
#wrapper {
max-width:98%;
width:auto;
margin:auto;
background-color: #fff;
}
#Prompt {
border-style: groove;
height: 400px;
max-width:98%;
width:auto;
position:relative;
overflow-y: auto;
}
#Buttons {
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
<div id="wrapper">
<div id="Prompt">
<br />
<!-- <p id="diceRoll">
</p>
-->
</div>
<br />
<div ="Buttons">
<form id="modTool" method="POST" action="">
<table>
<tr>
<td>
<td><b>USER</b> </td>
<td><select name="user" id="User">
<option id="PC10">0</option>
<option id="PC11">1</option>
<option id="PC12">2</option>
<option id="PC13">3</option>
<option id="PC14">4</option>
<option id="PC15">5</option>
<option id="PC16">6</option>
<option id="PC17">7</option>
<option id="PC18">8</option>
<option id="PC19">9</option>
<option id="PC110">10</option>
</select></td>
</td>
<td>
<td><b>ACTION TAKEN</b> </td>
<td><input type="text" name="actionTaken"></input>
</td>
<td>
<td><b>TARGET</b> </td>
<td><select name="target" id="target">
<option id="PC20">0</option>
<option id="PC21">1</option>
<option id="PC22">2</option>
<option id="PC23">3</option>
<option id="PC24">4</option>
<option id="PC25">5</option>
<option id="PC26">6</option>
<option id="PC27">7</option>
<option id="PC28">8</option>
<option id="PC29">9</option>
<option id="PC210">10</option>
</select></td>
</td>
</tr>
<tr>
<td>
<td><b>DICE SIDES</b> </td>
<td><select name="typeOfDice" id="typeOfDice">
<option id="D20">20</option>
<option id="D100">100</option>
</select></td>
</td>
<td>
<td><b>AMOUNT OF DICE</b>
<td><select name="amountOfDice" id="amountOfDice">
<option id="1Dice">1</option>
<option id="2Dice">2</option>
<option id="3Dice">3</option>
<option id="4Dice">4</option>
<option id="5Dice">5</option>
<option id="6Dice">6</option>
<option id="7Dice">7</option>
<option id="8Dice">8</option>
<option id="9Dice">9</option>
<option id="10Dice">10</option>
</select></td>
</td>
<td>
<td><b>BONUS</b> </td>
<td><select name="bonus" id="bonus">
<option id="plus0">0</option>
<option id="plus1">+1</option>
<option id="plus2">+2</option>
<option id="plus3">+3</option>
<option id="plus4">+4</option>
<option id="plus5">+5</option>
<option id="plus6">+6</option>
<option id="plus7">+7</option>
<option id="plus8">+8</option>
<option id="plus9">+9</option>
<option id="plus10">+10</option>
<option id="Minus1">-1</option>
<option id="Minus2">-2</option>
<option id="Minus3">-3</option>
<option id="Minus4">-4</option>
<option id="Minus5">-5</option>
<option id="Minus6">-6</option>
<option id="Minus7">-7</option>
<option id="Minus8">-8</option>
<option id="Minus9">-9</option>
<option id="Minus10">-10</option>
</select></td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name="submitRound" id="submit"></input>
</td>
<td>
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
EDIT:
Added overflow and made it autoscroll to bottom.
Related
I'm having trouble making dynamically added inputs required. especially with the "select" input
I have already tried manually checking (wo Jquery validate) if inputs submitted were correct but i encountered the same kind of problem. The "required" class doesn't help either.
Here's the html :
<form id='myform'>
<div>
<div id="addRow">+</div>
<div id="deleteRow">-</div>
</div>
<div>
<table id="tableex">
<tr>
<td>
<select name="selectbox[]" data-selected="" class='selectdyna required'>
<option value="" selected="selected" disabled="disabled">env :</option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
</td>
</tr>
</table>
</div>
<div>
<input type='submit' value='Validate'>
</div>
</form>
here's my js:
$(document).ready(function() {
$("#addRow").click(function() {
var str = "<tr>\n" +
" <td id=\"selecttd\">\n" +
" <select name=\"selectbox[]\" class='selectdyna required' data-selected=\"\">\n" +
" <option value=\"\" selected=\"selected\" >env :</option>\n" +
" <option value=\"1\">option1</option>\n" +
" <option value=\"2\">option2</option>\n" +
" <option value=\"3\">option3</option>\n" +
" </select>\n" +
" </td>\n" +
" </tr>";
$("#tableex").append(str)
$('#myform').validate();
$('.selectdyna').rules('add', { 'required': true });
})
$("#deleteRow").click(function() {
if ($("#tableex tr").length > 1) {
$("#tableex tr:last").remove();
} else {
alert("there must been one line minimum.")
}
})
})
here's a link to the fiddle: https://jsfiddle.net/v3tj2c5u/
I don't understand why you require the name of the dropdown that way.
You can do it as below demo
$(document).ready(function() {
$("#addRow").click(function() {
var count= $("#tableex tr").length+1;
var str = "<tr>\n" +
" <td id=\"selecttd\">\n" +
" <select name=\"selectbox"+count+"\" class='selectdyna required' data-selected=\"\">\n" +
" <option value=\"\" selected=\"selected\" >env :</option>\n" +
" <option value=\"1\">option1</option>\n" +
" <option value=\"2\">option2</option>\n" +
" <option value=\"3\">option3</option>\n" +
" </select>\n" +
" </td>\n" +
" </tr>";
$("#tableex").append(str)
$('#myform').validate();
$('.selectdyna').rules('add', { 'required': true });
})
$("#deleteRow").click(function() {
if ($("#tableex tr").length > 1) {
$("#tableex tr:last").remove();
} else {
alert("there must been one line minimum.")
}
})
})
Working demo
I'm having an issue with an assignment.
My for loop doesn't seem to run and I'm not sure what I've done wrong.
The variable dailyText prints "Weather Forecast" in a table but doesn't seem to enter the for loop. I'm not sure if this is a scoping issue or what's happening.
The script is supposed to take a number from a form option and create a table with that many rows.
Any help would be greatly appreciated!
function dailyInfo() {
var dailyText = "";
var numberDays = document.getElementById("numberDays").value; //https://stackoverflow.com/questions/23982774/turn-html-form-input-into-javascript-variable
dailyText += "<table class='table'><tr><th>Weather Forecast</th></tr>";
for (var i = 0; i < numberDays; i++) {
dailyText += "<tr><td>Day: " + (i + 1) + "</td>";
dailyText += "<td>" + dailyJsonObject.list[i].temp.min + "℃</td>";
dailyText += "<td>" + dailyJsonObject.list[i].temp.max + "℃</td>";
dailyText += "<td>" + dailyJsonObject.list[i].weather[0].description + "<img src ='http://openweathermap.org/img/w/'" + dailyJsonObject.list[i].weather[0].icon + ".png' /></td>";
}
document.getElementById("demo").innerHTML = dailyText;
dailyText += "</tr></table>";
}
<form id="numberDay">
<label id='numberDays'>Number of days requested</label>
<select name='numberDays'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select>
<button type="button" onclick="dailyInfo()">Submit</button>
</form>
</div>
<div id='demo'>
<table id="tableDaily"></table>
</div>
You have named the label with the id required for the numberDays. This means the loop was using the label value not the actual input as expected.
See below for alteration:
<form id="numberDay">
<label id='numberDays_label'>Number of days requested</label>
<!-- the select needs the id so the JS code can grab it!-->
<select name='numberDays' id="numberDays">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select>
<button type="button" onclick="dailyInfo()">Submit</button>
</form></div>
<div id='demo'>
<table id="tableDaily"></table></div>
You say:
var numberDays = document.getElementById("numberDays").value;
But actually the element you are getting is the label:
<label id='numberDays'>
So numberDays has actually the value undefined then.
Instead, you should do:
<label for='numberDays'>
and then:
<select name='numberDays' id='numberDays'>
That should actually at least start the loop. And then the label > select relationships is actually working, for example you can click now also the label to focus on the select.
Access a selected option:
HTML
<select name="" id="mySelect">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
JavaScript
var select = document.getElementById("mySelect"),
selectedOption = select.options[select.selectedIndex].text;
Get selected option text with JavaScript
In total (commented stuff)
function dailyInfo() {
var dailyText = "";
var numberDays = document.getElementById("numberDays");
numberDays = numberDays.options[numberDays.selectedIndex].text;
dailyText += "<table class='table'><tr><th>Weather Forecast</th></tr>";
for (var i = 0; i < numberDays; i++) {
dailyText += "<tr><td>Day: " + (i + 1) + "</td>";
dailyText += "<td>" + /*dailyJsonObject.list[i].temp.min +*/ "℃</td>";
dailyText += "<td>" + /*dailyJsonObject.list[i].temp.max +*/ "℃</td>";
dailyText += "<td>" + /*dailyJsonObject.list[i].weather[0].description*/ +"<img src ='http://openweathermap.org/img/w/'" + /*dailyJsonObject.list[i].weather[0].icon +*/ ".png' /></td>";
}
document.getElementById("demo").innerHTML = dailyText;
dailyText += "</tr></table>";
}
<form id="numberDay">
<label>Number of days requested</label>
<select id='numberDays' name='numberDays'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select>
<button type="button" onclick="dailyInfo()">Submit</button>
</form>
<div id='demo'>
<table id="tableDaily"></table>
</div>
I am trying to create a query which inserts the data in to Mobile sqlite data base say for now i have three rows in a html table on button press a query is generated where it selects the value of the text box and the select selected value but the problem is on print it should skip the last rows value as in my case that is a auto increment table where a last row is left blank to fill in the data for the user.
Now
INSERT INTO demo (xxxx, xxxxx, xxxx,xxxx,xxxx) VALUES ("Mickey1","Mouse1","No Match"),("Mickey2","Mouse2","No Match"),("skip row","skip row","No Match");
Expected
INSERT INTO demo (xxxx, xxxxx, xxxx,xxxx,xxxx) VALUES ("Mickey1","Mouse1","No Match"),("Mickey2","Mouse2","No Match");
Demo Jsfiddle
JS
function Get() {
var html = '';
var arr = [];
$('tr').each(function() {
var inputs = $('input', this);
var selects = $('select :selected', this);
arr.push('("' + inputs[0].value + '","' + inputs[1].value + '","' + selects[0].text + '")');
});
html = 'INSERT INTO demo (xxxx, xxxxx, xxxx,xxxx,xxxx) VALUES ' + arr.join(',') + ';';
$('#data').html(html);
}
HTML
<form id="Form" onsubmit="return false;">
<table>
<tr>
<td>First name:
<input type="text" name="FirstName" value="Mickey1">
</td>
<td>Last name:
<input type="text" name="Lastname" value="Mouse1">
</td>
<td>Last name:
<select name="select2">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</td>
</tr>
<tr>
<td>First name:
<input type="text" name="FirstName" value="Mickey2">
</td>
<td>Last name:
<input type="text" name="Lastname" value="Mouse2">
</td>
</td>
<td>Last name:
<select name="select2">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</td>
</tr>
<tr>
<td>skip row
<input type="text" name="FirstName" value="skip row">
</td>
<td>skip row
<input type="text" name="Lastname" value="skip row">
</td>
</td>
<td>skip row
<select name="select2">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</td>
</tr>
</table>
</form>
<input type="submit" name="Submit" onclick="Get();" />
<div id="data"></div>
If you add the index parameter to the each function you can edit your code to only display specific lines.
$('tr').each(function(index, me) {
if(index < $('tr').length - 1) {
var inputs = $('input', me);
var selects = $('select :selected', me);
arr.push('("' + inputs[0].value + '","' + inputs[1].value + '","' + selects[0].text + '")');
}
});
Also in each functions I believe this is not recommended. Adding the second argument to the function should act like this for the individual row (as I've have used me).
Here is a forked version of your Fiddle - https://jsfiddle.net/szkp0Lwq/1/
Edit
As per your comment below -
If you first want to validate your inputs you can do something like -
var valid = true;
$('tr').each(function(index, me) {
if(index < $('tr').length - 1 && valid) {
var inputs = $('input', me);
inputs.each(function(index, me) {
if($(me).val().length == 0)
valid = false;
});
var selects = $('select', me);
selects.each(function(index, me) {
if($(me)[0].selectedIndex == 0)
valid = false;
});
if(valid)
arr.push('("' + inputs[0].value + '","' + inputs[1].value + '","' + $(selects[0]).val() + '")');
}
});
if(valid)
..... submit query
else
alert("Form isn't valid!");
Fiddle - https://jsfiddle.net/o4b1rLns/2/
You could skip the row when you build the array, or only add the ones that doesnt have the "skip row" value, as below:
$('tr').each(function() {
var inputs = $('input', this);
var selects = $('select :selected', this);
if (inputs[0].value != "skip row") {
arr.push('("' + inputs[0].value + '","' + inputs[1].value + '","' + selects[0].text + '")');
}
});
I am working on an existing website which limits me to touch HTML code. In the code below, there are 3 rows with the same CLASS (sameRow),.Row numbers may differ from case to case. Inside each row contains DIV with unique ID and a corresponding SELECT element with the same CLASS (sameSelect).
My target output is to be able create a JS/Jquery code to store these unique div IDs individually in the array and display each ID in the alert. Currently, my code below is displaying only the ID of the first DIV. The second and the third is not displayed. What could be wrong in my code?
Any help is greatly appreciated. Fiddle link here -> https://jsfiddle.net/philcyb/crkLgxhd/
HTML:
<div class="sameRow">
<div id="uniquediv1">
<select class="sameSelect">
<option>1</option>
<option selected>2</option>
</select>
</div>
</div>
<br />
<div class="sameRow">
<div id="uniquediv2">
<select class="sameSelect">
<option>1</option>
<option>2</option>
<option>3</option>
<option selected>4</option>
</select>
</div>
</div>
<br />
<div class="sameRow">
<div id="uniquediv3">
<select class="sameSelect">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6" selected>6</option>
</select>
</div>
</div>
JS/JQuery:
$(document).ready(function(){
var optionValue = new Array();
var rows = document.querySelectorAll('.sameRow').length;
alert("there are " + rows + " rows");
for(i = 0; i < rows; i++ ) {
optionValue[i] = $(".sameSelect").parent().attr("id");
alert("array row number " + i + " is " + optionValue[i]);
}
});
Just use eq
$(document).ready(function(){
var optionValue = new Array();
var rows = document.querySelectorAll('.sameRow').length;
alert("there are " + rows + " rows");
for(i = 0; i < rows; i++ ) {
optionValue[i] = $(".sameSelect").parent().eq(i).attr("id");
alert("array row number " + i + " is " + optionValue[i]);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="sameRow">
<div id="uniquediv1">
<select class="sameSelect">
<option>1</option>
<option selected>2</option>
</select>
</div>
</div>
<br />
<div class="sameRow">
<div id="uniquediv2">
<select class="sameSelect">
<option>1</option>
<option>2</option>
<option>3</option>
<option selected>4</option>
</select>
</div>
</div>
<br />
<div class="sameRow">
<div id="uniquediv3">
<select class="sameSelect">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6" selected>6</option>
</select>
</div>
</div>
$(document).ready(function(){
var optionValue = new Array();
$('.sameRow').each(function(i){
optionValue[i] = $(this).find('.sameSelect').parent().attr("id");
alert("array row number " + (i + 1) + " is " + optionValue[i]);
});
});
JSFIDDLE
I think in this case no need for array you can use
$(document).ready(function(){
$('.sameRow').each(function(i){
var getID = $(this).find('.sameSelect').parent().attr("id");
alert("array row number " + (i + 1) + " is " + getID);
});
});
JSFIDDLE
I need to get the selected text and selectedIndex in dropdown on submit..The below code returns the first index always 0 with the first option text..for example if i selected three,it shows 0 index and one text...
function myfunc()
{
`enter code here`var x = document.getElementById("listt").selectedIndex;
var y = document.getElementById("listt").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}
<select id="listt" >
<option >one</option>
<option >two</option>
<option>three</option>
<option >four</option>
</select>
<input type="Submit" value="submit " name="Submit" onclick="myfunc()"></input>
Try this code.
<select id="listt" >
<option >one</option>
<option >two</option>
<option>three</option>
<option >four</option>
</select>
<input id="btnSubmit" type="Submit" value="submit " name="Submit" />
function myfunc() {
var x = document.getElementById("listt").selectedIndex;
var y = document.getElementById("listt").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}
document.getElementById ("btnSubmit").addEventListener ("click", myfunc, false);