Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
<input name="input1" id="input1" type="text" value="" class="problem">
<input name="input2" id="input2" type="text" value="" class="hidden">
I'm trying to populate a hidden text input (e.g. input#input2) field with the current value from another text input field (e.g. input#input1) (un-submitted) using jQuery. I've sourced this jsfiddle doing exactly what i require but with a drop down field. And i was wondering if anyone is able to point me in the right direction?
EDIT: Sorry if i wasn't clear, the jsfiddle works perfectly but i'm trying to use a text field instead of a drop down to populate the other field.
i always handle this by storing that value in a variable:
var yourText = document.getElementById("input1").value;
and then just plug it into the hidden field:
$('#input2').val(yourtext);
here's your hidden field:
<input id="input2" type="hidden" />
$("#my_form").submit(function(event){
var visibleInputVal = $("#input1").val();
if(visibleInputVal != ""){
$("#input2").val(visibleInputVal);
}else{
alert("you need to provide a value for input 1");
event.preventDefault();
}
});
<form id="my_form">
<input type="text" value="" id="input1" />
<input type="hidden" value="" id="input2" />
<input type="submit" value="Submit" />
</form>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to set text of input field using .value. After setting the value of input field, when I click on that field the text disappears. I don't want text to disappear.
The input html is as follow:
<input aria-invalid="true" aria-required="true" data-automation-id="address-form-firstName"
tealeafid="COAC2ShpAddrFirstName" name="firstName" initialvalue="" class="field-input field-input--
error field-input--primary" id="firstName" value="">
Setting the value of this input:
let name= document.querySelector("#firstName");
name.value = "First Name of user";
This html tag is present on Walmart website. Is there any solution?
If you want the text to be disappeared try placeholder property:
let name= document.querySelector("#firstName");
name.placeholder = "FirstName";
<input aria-invalid="true" aria-required="true" data-automation-id="address-form-firstName"
tealeafid="COAC2ShpAddrFirstName" name="firstName" initialvalue="" class="field-input field-input--
error field-input--primary" id="firstName" value="">
You should add an EventListener to your input
let name= document.querySelector("#firstName");
name.value = "FirstName";
name.addEventListener("click", function(){
name.value = "";
});
CodePen : https://codepen.io/abdelhedi/pen/qBEEEYw
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
If a user of my website inputs a value into an html text box and clicks on a submit button..
how can I transfer/use their value to javascript so I make a calculation based on their input.
Also how can I display their value on the site?
Thanks!
((New to javascript and stackoverflow))
You should have something like this in your HTML
<input name="searchTxt" type="text" id="txt" class="searchField"/>
And something like this in your JavaScript
var the_value_i_am_looking_for = document.getElementById('txt').value;
Note that the txt is the id of the input tag, and we use that same id to identify the element in JavaScript.
The variable the_value_i_am_looking_for should hold the value that you will use to make your calculation.
Example:
<input id="cost" type="number" />
<input type="button" onclick="calculate()" value="Enter a value" />
<div id="valueEntered" > </div>
<div id="afterTax" > </div>
<script>
function calculate()
{
valueEntered.innerHTML = " You entered : " + cost.value;
afterTax.innerHTML = " After tax : " + cost.value * 1.3;
}
</script>
You meant something like the following? It's a input box and a button. Once the button is clicked, the user input in the input box will be displayed below the input box and the button.
<input id="foo"/>
<button onclick="document.getElementById('bar').innerHTML = document.getElementById('foo').value
" type="button">Click me</button>
<div id="bar"></div>
</code>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to pass textbox value directly through query string the first variable is passed but the second value will be textbox value on the same page. Please help. Thanks
<input type="text" name="txtswap" id="txtswap" size="5" />
Swap
This can't be done with PHP alone, you need some JavaScript for it:
<input type="text" name="txtswap" id="txtswap" size="5" />
Swap
JavaScript:
function doSwap(self, event)
{
var swapValue = document.getElementById('txtswap').value;
event.preventDefault();
location.href = self.href + '&value=' + encodeURIComponent(swapValue);
}
<input type="text" name="txtswap" id="txtswap" size="5" />
<a onclick="swap()" >Swap</a>
<script>
function swap() {
var swapvalue = $('#txtswap').val();
window.location.href = 'UserForm.php?operation=swap&id='+swapvalue ;
}
</script>
Try this
Not tested...
Try this .
Swap
Write JavaScript function
function redirect(){
txtswap = document.getElementById("txtswap").value;
window.location.href = "UserForm.php?operation=swap&id="+txtswap;
}
Since the text box in the same page, you can't use PHP for this. PHP won't see the data (because it won't exist at the time the PHP runs).
Stop using a link and use a form instead, converting user input into a URLs is what they are for.
<form action="UserForm.php">
<input type="hidden" name="operation" value="swap">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row['id']; ?>">
<input name="txtswap" id="txtswap" size="5">
<input type="submit" value="Swap">
</form>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a form and it has several fields. There is an ID field which is generated from another form. Now i want to prevent users to change that field value.
How do i do this? DO i need any javascript or anything else?
You can use disabled on the field:
<input type='text' disabled />
Or readonly
<input type='text' readonly />
<input type="text" name="NAME-OF-FIELD" value="VALUE-NOT-TO-CHANE" disabled="disabled" />
or you could use
<input type="text" name="NAME-OF-FIELD" value="VALUE-NOT-TO-CHANE" readonly />
or if you need to hide it
< input type="hidden" name="NAME-OF-FIELD" value="VALUE-NOT-TO-CHANE" />
You will still have access to it when you submit the form.
If you can change the HTML, I'd suggest making the input hidden with type="hidden".
If you can't, apply the CSS style display:hidden to it.
Or did you want to leave it visible?
You have to disable the field:
<input type="text" disabled="disabled" />
Or set it readonly
<input type="text" readonly="readonly" />
A readonly element not editable, but gets sent when the form submits. a disabled element isn't editable and isn't sent on submit.Readonly elements can be focused while disabled elements can't.
<input type="text" readonly />
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to copy the index of a selection into a text field when the submit button is pressed. For example, I want to copy the name 'Accountant' of an option in the selection list into a textbox when I choose the option.
This is the HTML:
<form action = "">
<select name = "job_term_cat">
<option value="----">--Select--</option>
<option value="roma">Accountant</option>
<option value="torino">Cashier</option>
<option value="milan">Lawyer</option>
</select>
<br/>
<br/>
<input type="text" name="job_title" value="" />
<input type="submit" value="Test">
</form>
Here is the JavaScript I thought going to work for submit button, it returns no index of the option that is clicked.
<p><input type="submit"
onclick="var s= this.form.element[job_term_cat];
this form.elements[job_title].value= s.options[s.selectedIndex].textContent"
class="submit" name="job_submit"
value="<?php _e('Next →', 'myapp'); ?>" /></p>
var s= this.form.elements['job_term_cat'];
(mind the s in elements and the quotes)
this form.elements['job_title'].value= s.options[s.selectedIndex].textContent;
(mind the quotes)
well, you have some errors in your syntax, for example not var s= this.form.element[job_term_cat]; but var s= this.form.elements[job_term_cat];
here is the working code:
EDIT: I misplaced some names, check this:
<p><input type="submit" onclick="this.form.elements['job_title'].value = this.form.elements['job_term_cat'].selectedIndex" class="submit" name="job_submit" value="<?php _e('Next →', 'myapp'); ?>" /></p>