How to get the sheet object in the sheet properties? [duplicate] - javascript

This question already has answers here:
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 9 years ago.
How to get the form object?
I use this.box meaning sheet.box here, but the script produces an error. How do I get the sheet object in the sheet properties?
<div class="box">
<form action=""></form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var sheet={
box: $('.box'),
form: this.box.find('form') // TypeError: this.box is undefined
}
</script>

I guess what you need is a function:
form: function() {return this.box.find('form');}
Otherwise, you should be more specific.

Old versions of Firefox (I tried Firefox 4 which warns but still accepts it) actually allowed you to do this:
var sheet = {
box: #1= $('.box'),
form: #1#.find('form')
}

Related

Datatype .innerHTML [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 2 years ago.
I´m getting a NaN out of this script, is it a data type problem and how do i solve it?
<script>
function calc(){
var profile = document.getElementById("profil");
var hours = document.getElementById("hour");
document.getElementById("result").innerHTML=profile*hours;
}
</script>
Present the result like this
<button onclick="calc()">Calc</button><br>
<p id="result"></p>
You have to get the value from the element.
var profile = document.getElementById("profil").value;
With .getElementById you get a DOM Element.
Supposing "profil" and "hour" are input elements, you want to obtain their values:
<script>
function calc(){
var profile = document.getElementById("profil").value;
var hours = document.getElementById("hour").value;
document.getElementById("result").innerHTML=profile*hours;
}
</script>

localStorage.setItem() not working on page load [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 4 years ago.
I want to load a page and, after the body tag, execute a sort based on localStorage. But myWay (no pun intended) doesn't work. Can you help me?
I have 3 sort functions, each with one of these lines:
<script>
localStorage.setItem("current_sort", "classSort()");
localStorage.setItem("current_sort", "playerSort()");
localStorage.setItem("current_sort", "ratingSort()");
</script>
<script>
myWay = localStorage.getItem("current_sort");
if (myWay === "") {myWay = "playerSort()";}
</script>
How do I load myway?
<script>
eval (myway()); //doesn't seem to work.
</script>
that is how we execute functions as second parameter:
localStorage.setItem("current_sort", function(){classSort()});
localStorage.setItem("current_sort", function(){nameSort()});
localStorage.setItem("current_sort", function(){ratingsort()});

getElementsByClassName(className) returning null [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 6 years ago.
What to do if I have to insert some value on a page element of a new tab within a Chrome extension?
I am using:
document.getElementsByClassName("quotes").innerHTML = Quotes[x];
in newtab-script.js page but it is showing this error in the console:
Uncaught TypeError: Cannot set property 'innerHTML' of null
My page contains a div with a class named quotes but when I alert this:
document.getElementsByClassName("quotes")
The result is null.
My code is in the form:
window.onload = function abc (){
alert(document.getElementById('a'));
document.getElementById('a').innerHTML ="ishaan";
}
I am importing my script file in a html page which overrides Google Chrome's new tab page. My script is on the script file.
If there are multiple items having same class name
var quoteElems=document.getElementsByClassName('quotes')
for(var i=0;i<quoteElems.length;i++){
quoteElems[i].innerHTML=Quotes[x]
}
If there is single item having class 'quotes'
document.getElementsByClassName("quotes")[0].innerHTML = Quotes[x];
if you use jquery then do
$(function(){
$('.quotes').html(Quotes[x]);
});

jQuery cannot get input value [duplicate]

This question already has answers here:
How to select JSF components using jQuery?
(4 answers)
Closed 7 years ago.
I am trying to get input value after keypress by jQuery. I have tried few ways to get it and I receive undefined or "". Using JSF+RichFaces. Input is the <h:inputTextarea> Trying to get value by:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery("myform:idTextarea").val();
console.log(testValue);
});
});
</script>
The variable in log is undefined.
When I add "input:" like that
var testValue = jQuery("input:myform:idTextarea").val();
I get j_id12.
When change like that:
var testValue = jQuery("#myform:idTextarea").val();
I get "".
Have someone any clues?
You need to escape CSS meta-character while buidling the jquery selector:
jQuery("input\\:myform\\:idTextarea").val();
Escaping css meta character in jquery
You don't need to find same element for which keyup is registered, just use this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery(this).val();
console.log(testValue);
});
});
</script>

Determine if a variable is an empty jquery object [duplicate]

This question already has answers here:
How can I detect if a selector returns null?
(8 answers)
Closed 8 years ago.
Setup:
<div>
<div class="application-section active"></div>
<div class="application-section"></div>
</div>
I am setting a the following variables
var $activeSection = $("div.application-section.active");
var $targetSection = $activeSection.prev("div.application-section");
jQuery Documentation states that if no previous sibiling is found, it will return a blank jquery object. I want to do additional stuff if the variable is a blank jQuery object, but how do you check to see if it is a blank jQuery object? I have tried the following, but I always get false as a result.
alert($.isEmptyObject($targetSection));
$targetSection.length will be zero if no matching selector is found.

Categories

Resources