HTML Form that creates a string [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is kind of an odd one. Long story short, I'm trying to add strings together using dropdown lists but I'm not quite sure how to go about it and searching for answers has borne no fruit.
I've tried any number of combinations of jquery, java, and HTML but nothing works yet
Example:
Selection 1: first-string
Selection 2: second-string
Selection 3: third string
I also need it to display the result of this on screen somewhere (I was trying to run a function via a button that would add the strings together and display them in a text box but it would only add numbers, not strings)
EDIT: I felt i'd fundementaly misunderstood...well just about everything I tried which is why I didn't share the code. But here we go:
It is suggested you install a color picker to pair with this software
base = "000000";
eye = "FFFFFF";
nose = "000000";
m1c = "FFF000";
m2c = "00FFFF";
species = 1
m1 = 0
m2 = 0
function feli() {
var m1 = document.getElementById("textbox1").value;
var answer = "http://www.felisfire.com/demo.php?s="+species+"&b"="+"base"+"&e="+"eye"+"&n="+"nose"+"&m1="+"m1"+"&m1c="+"m1c"+"&m2="+"m2"+"&m2c="+"m2c";
var textbox3 = document.getElementById('textbox3');
textbox3.value=answer;
}
</script>
Species
<select name=species id=species>
<option value="1">Felidae</option>
<option value="3">Aquus</option>
<option value="8">Scalae</option>
<option value="5">Zerda</option>
<option value="6">Chetae</option>
<option value="10">Aurae</option>
<option value="7">Igneo</option>
<option value="9">Lycreon</option>
<option value="4">Iuridon</option>
<option value="2">Xano</option>
</select>
Marking 1
<select name=m1 onChange = "m1 = this.value">
<option value="1">None</option>
<option value="12">Accents</option>
<option value="41">Anubis (p)</option>
<option value="13">Appaloosa</option>
<option value="15">Back Spots</option>
<option value="124">Badger</option>
<option value="44">Ball Python</option>
</select>
Marking 2
<select name=m1 onChange = "m2 = this.value">
<option value="1">None</option>
<option value="12">Accents</option>
<option value="41">Anubis (p)</option>
<option value="13">Appaloosa</option>
<option value="15">Back Spots</option>
<option value="124">Badger</option>
<option value="44">Ball Python</option>
</select>
<input type="submit" name="button" id="button1" onclick="feli" value="Design!" />
<input type="text" name="textbox3" id="textbox3" readonly="true"/>
</div>

It's simple to combine the string using JQuery.
$("input").on("change", function () {
$('#combine').val(($('#str1').val() +" "+ $('#str2').val()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="str1" />
<input type="text" id="str2" />
<input type="text" id="combine" />
Hope it helps.

This code works.
If your HTML looks like this.
<select name="">
<option value="">thisis 1</option>
<option value="">thisis 2</option>
<option value="">tisis 3</option>
<option value="">thisis 4</option>
<option value="">thisis 5</option>
</select>
<select name="">
<option value="">thisis 1</option>
<option value="">thisis 2</option>
<option value="">thisis 3</option>
<option value="">thisis 4</option>
<option value="">thisis 5</option>
</select>
<select name="">
<option value="">thisis 1</option>
<option value="">thisis 2</option>
<option value="">thisis 3</option>
<option value="">thisis 4</option>
<option value="">thisis 5</option>
</select>
<textarea name="" id="resulter" cols="30" rows="10"></textarea>
and you jQuery would look like this.
<script>
$(function(){
$('select').change(function(){
// Get the selected option text
var getText = $(this).find('option:selected').text();
// Get the current textarea text
var areaText = $('#resulter').text();
// Concatenate the selected and area text together
$('#resulter').text(areaText + getText);
});
});
</script>

Related

Change a select option thanks to another option

What I'm trying is something pretty basic, but I can't do it because the examples I see aren't anything similar to what I'm looking for.
There are 2 select, one of them you choose manually and the other dynamically changes depending on the value of the first one.
If the value of the first select is 1, that in the second one they only appear whose value is 1 as well.
I want to make it 100% JavaScript, I don't want any JQuery.
HTML.php
<select onchange="catch_value_types()" name="types" id="types">
<option value="1">Meat</option>
<option value="2">Fish</option>
<option value="3">Vegetables</option>
</select>
<select name="food" id="food">
<option value="1">Pork</option>
<option value="1">Cow</option>
<option value="1">Chicken</option>
<option value="2">Sardine</option>
<option value="2">Salmon</option>
<option value="2">Mackerel</option>
<option value="3">Spinach</option>
<option value="3">Kale</option>
<option value="3">Green peas</option>
</select>
JavaScript.js
function catch_value_types() {
var types_value_option = document.getElementById("types").value;
// What should I put here?
}
Loop through the options and hide if value doesnot match
function catch_value_types() {
const selectedValue = document.getElementById("types").value;
const select2 = document.getElementById("food");
Array.from(select2.options).forEach((node) => node.style.display = node.value === selectedValue ? "block": "none");
}
<select onchange="catch_value_types()" name="types" id="types">
<option value="1">Meat</option>
<option value="2">Fish</option>
<option value="3">Vegetables</option>
</select>
<select name="food" id="food">
<option>Please Select</option>
<option value="1">Pork</option>
<option value="1">Cow</option>
<option value="1">Chicken</option>
<option value="2">Sardine</option>
<option value="2">Salmon</option>
<option value="2">Mackerel</option>
<option value="3">Spinach</option>
<option value="3">Kale</option>
<option value="3">Green peas</option>
</select>

JQuery Set selected option

I have two selection boxes, the default value is - and i want to pick something else for both, my first problem is both fields have dynamic id like prod-685209-Size so i'm having trouble accessing with id.
I have the following HTML:
<select class="sku-attr" name="Size">
<option value="_def">-</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<select class="sku-attr" name="Color">
<option value="_def">-</option>
<option value="Black">Black</option>
<option value="Blue">Blue</option>
</select>
So i executed the following:
document.getElementsByTagName('select')[0].selectedIndex = 2
document.getElementsByTagName('select')[1].selectedIndex = 2
It worked on the front side, showing my new options but it didn't prompt the backend as it is not actually selecting the options. Backend works fine when i click to these options by hand, basically i need another solution to choose these options.
If I don't misunderstood your requirement then you need something like this. Just add two different ids to your select element and attach a change event listener. For example size and color
var s = document.getElementById("size");
var c = document.getElementById("color");
function getSize() {
sizeSelected = s.value;
console.log('size=' + sizeSelected);
}
function getColor() {
colorSelected = c.value;
console.log('color=' + colorSelected);
}
s.addEventListener('change', getSize);
c.addEventListener('change', getColor);
<select id="size" class="sku-attr" name="Size">
<option value="_def">-</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<select id="color" class="sku-attr" name="Color">
<option value="_def">-</option>
<option value="Black">Black</option>
<option value="Blue">Blue</option>
</select>
You need to add .validate() after your dom commands to actually prompt the page.

how to validate userinput with javascript

I have this scenario where i have 4 drop down boxes, where you can choose CM for a carport and a shed, within that carport.
I want to prompt the user with messages/errors in these scenarios:
the shed is chosen to be wider than it is long.
the shed is atleast 60cm smaller than the carports in both width and length
only one of the sheds dimensions being chosen
the form looks like this
<form name="createorder" action="FrontController" method="POST">
<input type="hidden" name="command" value="createorder">
<br>
Length of shed:<br>
<select name="lengthShed">
<option value="0">I do not want a shed</option>
<option value="180">180cm</option>
<option value="210">210cm</option>
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
<option value="720">720cm</option>
</select>
<br>
Width of shed:<br>
<select name="widthShed">
<option value="0">I do not want a shed</option>
<option value="18=">180cm</option>
<option value="210">210cm</option>
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
</select>
<br>
Width:<br>
<select name="width">
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
<option value="720">720cm</option>
<option value="750">750cm</option>
</select>
<br>
Length:<br>
<select name="length">
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
<option value="720">720cm</option>
<option value="750">750cm</option>
<option value="780">780cm</option>
</select>
Bind a function to your <select> elements onchange property.
let selects = document.querySelectorAll('select');
for(var i = 0; i < selects.length; i++){
selects[i].onchange = function(){
// Check for your conditions
// If your warning conditions are met, prompt user
}
}
That is, assuming you want the validation to happen when the user changes one of the select's values.
If you want to validate with a button click, you can instead bind the function to the click event of the button.
let button = document.getElementById(buttonID);
button.onclick = function(){
// Check for your conditions
// If conditions are met, prompt user
}
To prompt the user, you can use a simple alert() to which you pass your message. Or make a more elaborate function to do something custom.
As for your conditions, that sounds like commercial math. Might have to split your question in multiple questions here as there are a lot of possible awnsers.
It might be easier for you to add IDs to your selects so you can get them via document.getElementById(idString). You can then get their value through document.getElementById(idString).value. And to use it in math solving, you will need to parse the string that the <select> will return as value, like so parseInt(document.getelementById(idString).value).

How to redirect user when clicking an option in a select list?

I'm kicking my self for not being able to do this. But I've tried almost everything. I simply want to redirect a user to a specific page when they click an option in my option value list. Here's my current code (which should explain my question better):
<select name="test_redirect">
<option value="1" onclick="document.location = 'http://localhost/shop?item=1';">Item 1</option>
<option value="2" onclick="document.location = 'http://localhost/shop?item=2';">Item 2</option>
<option value="3" onclick="document.location = 'http://localhost/shop?item=3';">Item 3</option>
</select>
I've also tried onChange as well. Same result. Can some one please help me with this? Thank you guys.
document.querySelectorAll("[name=test_redirect]")[0].addEventListener('change',
function () {
window.location = "http://localhost/shop?item=" + this.value;
});
This depends on a relatively new browser (with querySelectorAll and addEventListener), but the principle's the same. click doesn't get triggered on options, so you have to go with change on the <select>. Might as well consolidate the code a bit too.
http://jsfiddle.net/5n5ZE/
<select id="test_redirect">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
javascript
var val = document.getElementById("test_redirect").value;
window.location.href = "http://localhost/shop?item=" + val;
jquery
$("#test_redirect").change(function(){
var val = $(this).val();
window.location.href = "http://localhost/shop?item=" + val;
});
try this (give the link of the page in value of each option)
<select name="test_redirect" onchange="window.location.href=this.form.test_redirect.options[this.form.test_redirect.selectedIndex].value">
You need to bind the event handler to the <select> and not the individual options:
<select name="test_redirect" onchange="location.assign('http://localhost/shop?item=' + this.options[this.selectedIndex].value)">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
This will save you lines :
<select onchange="location = this.options[this.selectedIndex].value;">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
try this
<select name="test_redirect" onchange="location.href='http://localhost/shop?item='+this.value">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>

How do I get input value and Selected Value of dynamically created inputs using JavaScript or JQuery in IE8 and IE9?

I have this working in IE 7 using getElementById() but in IE8 and IE9 that does not work. What I am doing is I need to do validation on the select boxes to see that they are required and check what they selected to do other things. I also need to check the input boxes to see what the value is and it is required as well.
function saveTest(){
var startNumber=0;
var endNumber=3;
// Works
alert(document.myForm.mySelect_1.value);
for (i = startNumber; i <= endNumber; i++) {
// I know this is not right but I am trying everything
alert(document.myForm.mySelect_+i.value);
// Using JQuery to try to solve the issue and this does not work for me.
alert($('#mySelect_'+i).val());
}
}
I know these are not dynamically created belwo but this is just to help me find the answer to this issue. I am creating more selects
<input name="myText_1" id="myText_1" value="">
<br />
<select name="mySelect_1" id="mySelect_1">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
<input name="myText_2" id="myText_2" value="">
<br />
<select name="mySelect_2" id="mySelect_2">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
<input name="myText_3" id="myText_3" value="">
<br />
<select name="mySelect_3" id="mySelect_3">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
Save
If I am not clear enough let me know. I will do my best to explain anything you do not understand.
Thanks for your time.
The following line is not correct; you are trying to concatenate a variable name, called mySelect_ to an integer:
alert(document.myForm.mySelect_+i.value);
Instead, you should use the associative array syntax:
alert(document.myForm['mySelect_'+i].value);
Also, within this same loop, you should start from 1 instead of 0, since there's no element called mySelect_0.
Here's a working DEMO.
You have created drop downs with Names
Try using
$('select[name=mySelect_'+i+']').val();
else
$("#id option:selected").text()

Categories

Resources