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 have a java script, getting domain name of webpage like this:
<script>
window.onload = function getDomain()
{
var x = location.hostname;
document.getElementById("domain").innerHTML=x;
document.getElementById("Field25").value=x;
}
</script>
and I have a text box, that I need the x to be its value, like this:
<input id="Field25" name="Field25" type="url" class="field text medium" maxlength="255" tabindex="4" required />
its not working
what should i do?
You're telling it to make a string "x" the value of field25. Just use the variable x.
document.getElementById("Field25").value = x;
Get rid of the window.onload. You've got the syntax wrong anyway. You're trying to do
window.onload = function() {
// Some code here
}
You're currently redefining window.onload as the function getDomain(), then never calling getDomain().
If you get rid of the window.onload completely, the code will do what you want.
Example: http://jsfiddle.net/qLhn8/
Aside: It's possible that window.onload isn't running under the context of jsfiddle, so you may need it after all.
I don't quiet understand, but if you mean that you want the input#Field25 to have the value from the x variable, look closer because you are assigning the string 'x', not the value from the variable x
Update your code to this:
document.getElementById("Field25").value = x;
Related
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 5 years ago.
Improve this question
Hi I want to pass a variable to queryselectorAll. I tried a couple of things but non have worked for me. I'm doing that because in my code I change the variable to a new one every time a click a button.
var container = document.getElementById(containerBox.id)
// I want containerBox.id to be called in querySelectorAll
templateDiv = document.querySelectorAll('#' + 'containerBox.id' + 'template')[0].content.firstElementChild
Thanks in advance
This question can be simplified to: How do I construct a string from fixed parts and a variable part?
The answer to that is:
'#' + containerBox.id + 'template'
(i.e. just don't put quotes around your variable name).
But why bother using .querySelectorAll() just to grab the first index? You could simply call .querySelector() instead.
And if all you need to get is an element with a specified id, you can just do this:
document.getElementById(containerBox.id + 'template')
... which is the method you're already using in your first line.
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 6 years ago.
Improve this question
I've got an object.
function Obj()
{
}
Obj.prototype.doSomething = function(thing)
{
this["do" + thing]();
}
Obj.prototype.doAlert = function()
{
alert("Alert!");
}
var obj = new Obj();
obj.doSomething("Alert");
This is just a shortened down version of my object, and is a lot bigger.
What I would like to do is that if you pass in 'Alert' it will run this.doAlert(); and if I pass in 'Homework' it will run this.doHomework();
Obviously, in this case, it is stupid to do it like this, but my final project is going to be completely different.
It works fine with window like this:
window["do" + thing]();
but I don't want it to be a global function, but to be part of obj.
Does anyone have an idea how I'd go about doing this?
Thanks in advance!
It turns out that when you get the function through this['functionName'], this is not bound to it.
This means you cannot use this.foo inside any function called like above.
To fix this, I used the following code:
this["do" + thing].bind(this)();
instead of this["do" + thing]();
JSFiddle: https://jsfiddle.net/auk1f8ua/
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 have a Javascript setInterval function that works in Chrome, but not in Firefox. It is supposed to keep writing a string every so often on the screen. I know document.write is not a prefer method. Here is the code:
function doSomething(){
document.write("1st string ");
}
setInterval(doSomething, 2000);
Thank you (JS newb).
When using document.write in firefox, you need to have document.open it first, you can read about it on MDN
Also, no one uses document.write anymore, and if they do, that is just redundant.
If your target is just to write strings into the body, use something like this:
function safeDocumentWrite(text) {
document.body.appendChild(document.createTextNode(text));
};
or alternatively if you want to append HTML, wrap it in a <div> first:
function safeDocumentWrite(html) {
var div = document.createElement('div');
div.innerHTML = html;
document.body.appendChild(div);
}
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
HTML
<div>
<input ng-model="tripsheet.tripsheet_num" type="text">
</div>
JS
$scope.getTotalTripsheets = function()
{
return $scope.tripsheets.length;
};
i want the above data(getTotalTripsheets) to to default input in the input tag, in the above code i am fetching the length from the database table and tried making it as the default data in the input field.
i tried somthing like this(below), it dos not work, how should i proceed with this?
$scope.tripsheet = function()
{
$scope.tripsheet_num = !$scope.getTotalTripsheets;
};
If you just want a one-time/one-way bind to the length of an Array, use ng-value instead of ng-model:
<input ng-value="tripsheets.length" type="text">
If you have a two-way binding that can be changed from the default:
$scope.tripsheet = {
tripsheet_num: $scope.tripsheets.length
};
<input ng-model="tripsheet.tripsheet_num" type="text">
Example in plunker: http://plnkr.co/edit/UM4lyGZSxB1mEcpy9CqJ?p=preview
Finally, it sounds like you may be doing a remote call to a database, so you could use the following strategy:
$scope.tripsheet = {
$resolved: false,
tripsheet_num: null
};
When the remote call succeeds, set $resolved to true and also set the value of tripsheet_num. In the HTML you could do something like this:
<input ng-disabled="!tripsheet.$resolved" ng-model="tripsheet.tripsheet_num" type="text">
Resolve example on plunker: http://plnkr.co/edit/q2Ua8KGqWr6HDphe92q0?p=preview
you are making you input a model of tripsheet.tripsheet_num which means that on the $scope there is going to be a property called tripsheet.tripsheet_num (I think, not sure because of the . what it will actually do.)
However i do know that when you make the tripsheet num a function it overrides whatever it was before and you should lose the .tripshee_num property. Your structure should be more like this.
$scope.tripsheet = {}
$scope.tripsheet.tripsheet_num = "default value";
if tripsheet is an array then change the model on you input to be "tripsheetLength" and change code to this.
<input ng-model="tripsheetLength" type="text">
$scope.tripsheets = ['info1', 'info2']
$scope.tripsheetLength = $scope.tripsheets.length; // length here is 2
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 9 years ago.
Improve this question
I have the following field,
<input id="department" value="finance"/>
I'm trying to create an object variable containing the field in plain old javascript like so.
var $field = $(document.getElementById("department"));
I'm then trying to pull the value from the field variable, however it's failing.
$field.value;
Does anybody know what I'm doing wrong?
That looks like a jQuery object, which is a collection of DOM elements, and the value property is attached to the DOM elements.
You should have:
var field = document.getElementById("department");
// now you can access field.value
jQuery objects have a val() method that gets you the value of that property:
var field = $('#department');
// here you can call field.val();
Lose the call to $() if you want a DOM element, and not a jQuery object:
$field = document.getElementById("department");