I don't know if this can be do it, i have a input with a submit:
Variable JSON (data/data.links)
<input type="text" id="newVarJson" value="data" onclick="verifica();"/><br>
and I need to put the value of that input as the name of a variable in javascript to create a json with that name, i thought on make that:
<script type="text/javascript">
function verifica(){
var document.getElementById(newVarJson).value={}
data.value="asdf";
alert(JSON.stringify(data));
}
</script>
As you see, the variable the input value = data, I don't know if this can be do it, so, can someone tell me any tips to make this, or it can't be do it?
Thanks!
You can try this:
window[document.getElementById(newVarJson).value];
and your var name will be the value of that input.
Edit:
window[document.getElementById('newVarJson').value];
Working jsbin: http://jsbin.com/doparibu/1/edit
Related
I'm trying to assign a value to a hidden form field, the value comes from a query string parameter. The function to extract the query string parameter works fine, however the function to assign the variable (using document.forms) to the hidden form field value attribute doesn't seem to work, the value is empty if I inspect element, however it works if I run it through the console in Chrome. Many thanks.
Get variable from function that finds query string:
var actionCode = getAllUrlParams().actioncode;
Set hidden form field value:
function setHidden()
{
document.forms[0].action.value += actionCode;
return true;
}
Form HTML:
<input id="field25" name="action" type="text" value="" class="field-size-top-large" disabled="disabled">
Live page is here: http://exhibit.ubm-events.com/LP=83?cid=sm(n)_VIS_DRV20180515%7C1&actioncode=EMA1234
pass the actionCode variable to the function so that it's definitely in scope, and use .getElementById seeing as the element has an ID.
function setHidden(actionCode)
{
document.getElementById("field25").value += actionCode;
return true;
}
Try below code
function setHidden(a,b){
return a*b;
}
document.getElementById('field25').value = setHidden(2, 3);
you need to select the element using jquery and assign the value to it
$("#field25").val("your value")
Ref http://api.jquery.com/val/#val2
I've spent days looking for an error in PHP only to discover my jQuery was never passing the correct value via AJAX in the first place. I was 100% sure this worked and have tested in the past:
JS:
var barcode = null;
if ($("#barcode").length)
{ var $barcode = $("#barcode").val(); console.log("barcode"); }
alert(barcode);
HTML:
<div class="col-lg-10">
<input type="text" class="form-control" id="barcode" placeholder="Barcode">
</div>
Why does barcode still equal null when there are values in the barcode input?
Because the variable you are assigning a value to is
$barcode
and you alert
barcode
Like in real life $ matters
You are creating a new variable, called $barcode inside of the if block. Your worker has no idea that you meant the barcode variable defined above. Instead, try barcode = instead of var $barcode=
You are alerting barcode which is null. You would need $barcode
I've created a JavaScript function that checks if a certain data already exists in my database. What I want to know is if there is a way to make the input field name in a JavaScript pass as an argument
Here is my code
function checkDataAvailability(displayid,input_id,fieldname)
{
'use strict';
$(document).ready(function(){
//var x = document.getElementByName(fieldname).elements;
$(displayid).load('php/signcheck.php').show();
$(input_id).keyup(function(){
},
$.post('php/signcheck.php', { username: form.x.value },
//$.post('php/signcheck.php', { username: form.fieldName.value },
function(result){
$(displayid).html(result).show();
});
});
});
}
var a = checkDataAvailability ('#userstat','#username_input','username');
A little explanation. The two commented lines are the two methods I've tried to run the field name as an argument separately. Unfortunately they aren't working.
Here is my form
<form action="php/register_exec.php" method="POST" name="form">
Username <span id="userstat" class="checkerr"></span>
<input type="text" name="username" id="username_input" required>
</form>
Passing form fieldnames as argument is no different than passing string argument to functions
var a = checkDataAvailability ('userstat','username_input','username');
Important thing is what you do inside the function.
You can get the value of input field in primarily two ways
Directly read the value using value property as:
document.getElementById('username_input').value
or
document.getElementById(fieldid).value //if you pass fieldid to your function
Use the form field directly
//assuming you pass formname and fieldname as variables to your function
var form = document.getElementById(formname);
var inputvalue = form.elements.namedItems(fieldname).value
You can modify them to suit your jquery syntax if need be.
Since you're already using the jQuery library, you can continue using it.
$('input[name="' + fieldname + '"]').val()
There are 3 ways of achieving what you desire -
If you want to stick with your current code pattern, then replacing form.fieldname with form[fieldname] would get you the correct results. This is because fieldname is a string, and form."some string" would give you an error.
The other two ways are the same as neouser99 and avck specified in their answers.
I am trying to get the value of a field on a form when I call the update function.
function update(gId, name, status){
alert(gId);
alert(name);
alert(status); \\Works fine and displays the proper element name.
alert(document.Form.status.value);\\Try to get the value of that element and it returns undefined.
}
The gId, name and status are all Strings of elements Id's being passed into the update function. I have 3 dynamically created input fields that get updated. Ex: i_name, i_status, i_gid where i can be 0 or more. So when I call this update Im really passing in a string like 0_gid, 0_name, 0_status or 999_gId, 999_name, 999_status..ect.
pseudo form code.
<form>
input id&name=3_gId
input id&name=3_name
input id&name=3_status
Update(3_gId, 3_name, 3_status)
input id&name=11_gId
input id&name=11_name
input id&name=11_status
Update(11_gId, 11_name, 11_status)
</form>
Thanks for any help.
Try just doing...
var text = document.getElementById(status).value;
alert(text);
or just put the document.getElementById(status).value in the alert
I have some jQuery inside a text field which I need to fire, but I'm struggling to get anything to happen.
I think the best way to explain this is to put down some code:
<input type="hidden" id="win-body-tst" value="$("#a").append($("#b"));">
<script> function NNNfoo(){
var funcvar = document.getElementById('win-body-tst').value;
funcvar
}</script>
I am calling NNNfoo() successfully, but the append does not action.
eval will run JS code which is expressed as a string (but I'd take a step back and try to solve whatever problem you have another way, it isn't usually a good idea to ask users to input JS).
eval(funcvar);
(You also need to convert your " as data to " so that they don't act as attribute value delimiters).
You can do it with
function NNNfoo(){
var functionBody = document.getElementById('win-body-tst').value;
var func = new Function( functionBody );
func();
}
But why do you want to run arbitrary code from input elements ?