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

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.

Related

Why/how is each div with an `id` callable from js? [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Is there a spec that the id of elements should be made global variable?
(5 answers)
Closed 9 months ago.
While coding some js for a webpage, I had initially written a function to parse every div with an id and assign it to a variable for easy targeting:
var div={};
for (let e of Array.from(document.getElementsByTagName('div'))
.filter(div => div.id)) {
div[e.id] = e;
};
But while debugging I suddenly realized that js recognized all these divs "out of the box" and that I could call them directly. For example, if my HTML contains
<div id='status'>
<div id='modtime'></div>
<div id='client'></div>
</div>
Then I can simply do modtime.innerText='abc' – without having to assign modtime via something like div.modtime = document.getElementById('modtime')
Would like to know more about this 'functionality'; cannot find any documentation on it. Not sure what it's called.

Javascript new feature? [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Is there a spec that the id of elements should be made global variable?
(5 answers)
Closed 9 months ago.
I wanted to know if its new feature or just a working bug
Before I used to do like
HTML :
<div id="identity">I am an demo text</div>
JS :
let iddiv = document.getElementById("identity");
console.log(iddiv)
but Now I can do
console.log(identity)
Withour storing the element in a variable
I just wanted to ask if it is fine !!
console.log(identity)
<div id="identity">I am an demo text</div>

Why can I not find using data attribute [duplicate]

This question already has answers here:
jQuery Data vs Attr?
(3 answers)
Closed 5 years ago.
In code I do this:
var markerName = $(fileInput).closest('tr.file-input-row').find('input[type="text"]')[0].value.replace(/[^a-z0-9]/gi, '-');
$.data(fileInput, 'for', markerName);
in this case, markerName is "file-1"
If I check using:
$('input[type="file"][data-for="file-1"]')
I get an object with length equal to 0... so not found.
However, if I do:
$('input[type="file"]:first').data().for
in this case, the first input[type="file"] is the same input I set the data attribute for, I get:
"file-1"
...as expected.
It looks like it is being set, but it is then not accessible.
Any thoughts why?
TIA
This is not working because you aren't updating the DOM object when you perform the data-for adjustment in the first part of your code. To update the DOM object you should use attr(key, value).
For more info on the differences between data and attr there is a good answer related to this: jQuery Data vs Attr?
If I check using:
$('input[type="file"][data-for="file-1"]')
Neither .data() nor jQuery.data() is part of the object returned by jQuery() : $() call.

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

find element by it's attribute value [duplicate]

This question already has answers here:
jQuery how to find an element based on a data-attribute value?
(9 answers)
Closed 7 years ago.
$('a').filter(function () {
return $(this).attr('area-expanded')
});
will return an array of links with attribute called 'area-expanded'.
I need to find a link, whose attribute 'area-expanded' value is true (there is always one such link on the page).
I tried the following:
$('a').filter(function(){return $(this).attr('area-expanded').val() == true});
but I am getting
Uncaught TypeError: Cannot read property 'val' of undefined
I'm complete noob in JS so I am sure there is something simle I am missing.
Thanks!
Use attribute-value selector:
Selects elements that have the specified attribute with a value exactly equal to a certain value.
$('a[area-expanded="true"]')
This will select all the <a> whose aria-expanded attribute value is set to true.

Categories

Resources