I want to get a value from JavaScript prompt and pass it to input for finally submit it
HTML
<form id="createdirForm"><input type="text" name="" id="createdir"/><form>
JavaScript
function createdir() {
var newdirname;
newdirname = prompt('Please input the directory name:', '');
if (!newdirname) return;
$('createdir').newdirname.value = newdirname;
$('createdirForm').submit();
}
The reason why it's not working is because you're not calling the selector in your jQuery properly.
You might want to take a look at the jQuery Selector Tutorial.
Now to fix your problem, since you want to get the input and the form by the id attribute, just like in CSS, you need to use the hashtag (#) symbol.
So you would do:
$('#createdir').val(newdirname);
$('#createdirForm').submit();
Here's a working example: https://jsfiddle.net/vm4ah1L3/1/
Just a side note, you might want to add a name to your html input otherwise when you submit it to your PHP, you won't be able to retrieve the value since the name attribute is empty.
<input type="text" name="dirName" id="createdir">
And in your case, since you're declaring the variable newdirname and assign it right after, you could simply do the assignation in one line:
var newdirname = prompt("Please input a directory name: ");
It seems your jQuery selector is wrong. Try
$('#createdir').val(newdirname);
$('#createdirForm').submit();
jQuery uses the css selector logic, therefore to select an element by id you need to put the # before the selector name.
Also, to add a value to an input, you need to use val(param) with your value replaced in the param.
You have some typo and have forgotten to add #. And the function is not executed.
HTML
<form id="createdirForm">
<input type="text" name="dir" id="createdir" />
</form>
JavaScript
function createdir() {
var newdirname;
newdirname = prompt('Please input the directory name:', '');
if (!newdirname) return;
$('#createdir').value = newdirname;
$('#createdirForm').submit();
}
createdir();
Related
I need to get the value of an <input>, specifically the stuff that is held inside its value attribute.
However, the input is not visible, so that seems to be a problem for testcafé.
Does anyone know how to work around that? Is there a special option you can use with the Selectors to make it work?
Thanks for helping me out, I appreciate any help!
Got it, simply declare a Selector like this let yourInputs = Selector('input[type="hidden"]'), this will get all hidden inputs and return a NodeList which you can iterate over in your test.
If you want to be more specific and select over an ID or name, do it like #lumio.
Then you can access the value in your test run with an await yourInputs.value.
I guess you mean a hidden input element as in <input type="hidden" /> and you want to receive the value before you're sending it to your Node application. You can use querySelector for this.
console.log( document.querySelector( 'input[name=test]' ).value );
<input type="hidden" name="test" value="hello world" />
For TestCafé you got the Selector-constructor which creates a selector.
As fweidemann14 pointed out, you can do the following:
const hiddenInputs = Selector( 'input[type="hidden"]' );
I have input field like
<input type="text" name="time[23][34][]" value="">
name of input field is dynamic. Means it might be time[23][34][],time1[11][33][],time2[45][22][] etc
Now I want to change it to multidimension to singledimension like
time[]
time1[]
time2[]
I have tried below code:
$('input').attr('name').replace(/(\[|\])/g, '\\$1')
How can i replace it using jquery or javascript?
this may help you:
https://jsfiddle.net/s033kv43/
var attrib = $('input').attr('name').replace(/[\[|\]]/g, '');
$('input').attr('name', attrib);
i get the name attribute and replace the brackets with nothing, then set the name attribute.
Dont forget to adjust this code, so its alters the correct input-fields
For some reason clicking the button will not but the variable in the textbox, I'm not getting any syntax errors in my compiler so I think it's a logic error. THanks in advance
JS:
var testvar = 5
$('.teambtn').click(function() {
document.getElementById('team').text = (testvar);
});
html:
<textarea id=team cols="50" rows="10">
Your Team Will be here:
</textarea>
<button type="button" id=teambtn class="btn">Export</button>
I think you're almost there. You just need to remove the brackets from test var and instead of replacing the .text property, you need to replace the .value property
document.getElementById('team').value = testvar;
Check out this link because I think it accomplishes what you want, http://www.mediacollege.com/internet/javascript/form/add-text.html
i think the problem is below .
$('.teambtn')
try to use $('#teambtn') .
Because you declared it is an id . But the selector indicates to class .
If you do not want to change the .js code then you can add it as a class .
I hope this will helps you .
Since you have used jQuery, below is sample usage:
var testvar = 5
$('#teambtn').click(function() {
$('#team').val(testvar);//set the value of textarea
});
teambtn and team are ids given, so to fetch by id using jQuery's selector #
To get/set the value of textarea and input elements use jQuery's $.val()
EDIT
Problem with your code is that you are trying to set text property of textarea, which is not valid instead you must try using value attribute:
document.getElementById('team').value = testvar;
Check input tag which have value only as <br/> tag inside as value using jQuery
I am using an htmlEditor in my project, sometimes user just press enter key only in textarea field. It will get as <br/><br/><br/><br/> as value when saving.
I want to avoid this tags when saving.
How to validate this in jquery ?
If any other characters like <br/>Just <br/> Example <br/> is present then I want to save it with break tag itself.
using a regex like this would help.
/^(\<br\/>)+$/.test($("textarea").val());
Clean it like this:
// Get the HTML and remove <br /> variants
var htmlCleaned = $('#MyTextArea').val().replace(/<br\s?\/?>/, '');
Refer LIVE DEMO
var divTag = $("div");
var nDivTag = $(divTag[0].outerHTML);
$("br", nDivTag).remove();
alert(nDivTag.html());
I have a text box element whose value I am trying to access using document.getElementById("id-name").value. I find that the call is returning a null instead of empty string. The data-type of the returned value is still string. Is null a string value?
<input type="text" value="" id="mytext"> is the textbox whose value I am trying to fetch using var mytextvalue = document.getElementById("mytext").value;
Posting your HTML might help a bit. Instead, you can get the element first and then check if it is null or not and then ask for its value rather than just asking for the value directly without knowing if the element is visible on the HTML or not.
element1 = document.getElementById(id);
if(element1 != null)
{
//code to set the value variable.
}
fyi, this can happen if you are using the html type="number" attribute on your input tag. Entering a non-number will clear it before your script knows what's going on.
For your code
var mytextvalue = document.getElementById("mytext");
mytextvalue will contain null if you have a document.write() statement before this code. So remove the document.write statement and you should get a proper text object in the variable mytextvalue.
This is caused by document.write changing the document.
It seems that you've omitted the value attribute in HTML markup.
Add it there as <input value="" ... >.
Please check this fiddle and let me know if you get an alert of null value. I have copied your code there and added a couple of alerts. Just like others, I also dont see a null being returned, I get an empty string. Which browser are you using?
This demo is returning correctly for me in Chrome 14, FF3 and FF5 (with Firebug):
var mytextvalue = document.getElementById("mytext").value;
console.log(mytextvalue == ''); // true
console.log(mytextvalue == null); // false
and changing the console.log to alert, I still get the desired output in IE6.
I think the textbox you are trying to access is not yet loaded onto the page at the time your javascript is being executed.
ie., For the Javascript to be able to read the textbox from the DOM of the page, the textbox must be available as an element. If the javascript is getting called before the textbox is written onto the page, the textbox will not be visible and so NULL is returned.
try this...
<script type="text/javascript">
function test(){
var av=document.getElementById("mytext").value;
alert(av);
}
</script>
<input type="text" value="" id="mytext">
<input type="button" onclick="test()" value="go" />
if you are using external js file add <script src="fileName.js"></script> at the end before closing the </html> tag. or place <script> at the end before closing html tag .