css attributes all blank javascript [duplicate] - javascript

This question already has answers here:
Can jQuery get all CSS styles associated with an element?
(5 answers)
Closed 5 years ago.
I am using jQuery to edit html elements using .each on all off a class. If I try to log any of the attributes then I will just get "" even though I have set the attributes to something in my css file.
Why is this happening? Can the javascript file not access the css attributes?

As I'm unclear whether you mean CSS attributes or DOM element attributes, here's an explanation for both:
To loop through some elements before reading their (in this case value) attributes, you can do this like:
$('.element').each(function(){
console.log( $(this).attr('value') );
});
To loop through some elements before reading their CSS properties, you can do this like:
$('p').each(function(){
console.log( $(this).css('width') );
});
Take a look at the console to see the three elements width being output: https://jsfiddle.net/nrfxn9nb/

Related

Is there any way to add non-element-contained text to a $("")? [duplicate]

This question already has answers here:
How to append/prepend/create a text node with jQuery
(3 answers)
Closed 6 years ago.
I'm creating a JavaScript framework to build HTML documents. First a virtual document is built using jQuery. Right now, I'm experimenting with jQuery's "add" function like so:
$(target).append($("").add($("<div>")).add($("<span>")))
The framework concatenates these calls to build the virtual document before it is appended to the target - this simplified code sample isn't literally what I'm doing. The reason for adding the first $("") is because the framework starts by creating an empty jQuery selection then adds stuff to it. Sub-documents are recursively created and added to parent elements.
This works fine for concatenating elements together, but what if I want to concatenate text? Let's say I want to have something like this rendered:
<div></div> Outside the box!
I can't just do $("<div>").add("Outside the box!") Also, $.after() doesn't seem to work unless the <div> is already on the DOM.
Is this functionality supported by jQuery? If not, are there any workarounds?
Yes, you can use simple string concatenation with current HTML of element: $('<div>').html($('<div>').html() + 'Outside the box!')
Since your code DOM is in memory and not actual HTML, you need to use multi-line code:
var $div = $('<div>', {html: $('<div>')});
$div.html($div.html() + "Outside the box!");

Find all elements with an 'id' attribute on the DOM [duplicate]

This question already has answers here:
jquery get only all html elements with ids
(3 answers)
Closed 8 years ago.
Simple. I want to traverse the DOM and find all elements that have an id attribute. Can someone help me with a js and/or jquery script
You can query all the elements with an id attribute by using the following jQuery selector:
$("[id]")
You can also just use plain JavaScript using querySelectorAll:
document.querySelectorAll("[id]")
If you want to find non-empty id attributes, use this selector instead:
'[id]:not([id]="")'
Simple attribute selector
$('[id]');
Reference : Has Attribute Selector [name]

Unable to edit CSS style using Javascript [duplicate]

This question already has answers here:
Overriding !important style
(11 answers)
Closed 8 years ago.
I have an HTML element that looks like this:
<p style=​"font:​12px verdana !important;​margin:20px !important;​">​…​</p>​
Now at some point I need to edit the margin attribute through Javascript. The code looks something like this:
document.getElementsByTagName('div')[3].getElementsByTagName('p')[0].style.margin='0'
document.getElementsByTagName('div')[3].getElementsByTagName('p')[0].style.marginLeft = '150px'
However, this isn't working. After I check the value of 'margin' and 'marginLeft' before and after the code, the values remain the same. If I use Google developer tools and manually change the Javascript then the issue is resolved. Can anyone tell me why the code above is not changing the value of the style? I also tried using setAttribute but that did not work either.
You can try this with jquery:
$('selector').css('cssText','font:​12px verdana !important;​margin-left:150px !important;');
making of this way, it overrides your inline style, so be careful if you modify your style dinamically
You can use jquery for this its very easy:
$( ".selector" ).css( "background-color","#fff" );
OR you can access your element in this way
var elements = $( "body" ).find("div");
var p = elements[3].find("p");
$p[0].css("margin","0px");
$p[0].css("margin-left","150px");
if you don't want to use jQuery then you first check that you are accessing/selecting the right div and p tag you can alert the length of elements found and check is 3rd element have the one which you want to update?

Editing an item's style via JavaScript? [duplicate]

This question already has answers here:
Changing CSS Values with Javascript
(9 answers)
Closed 8 years ago.
I'm looking for help regarding how to edit an object's style in a separate CSS file via JS. For example, if I have an object that I style with #objectid {left: 0%;} in my CSS file, how would I go about changing that left property via JavaScript? I'm aware you can do object.style.property but that hasn't been working for me as of late. What's the most efficient/easy method of doing this? Thanks in advance.
You have to get the element in the DOM
with pure javascript:
document.getElementById('objectid').style.left = '20%'
With jQuery
$('#objectid').css('left', '20%');
or
$('#objectid').css({'left': '20%'});
The standard way:
document.getElementById('objectid').style.left = "10%";
But if you're worried about efficiency, try not to modify any styles directly using JS... try adding/removing classes instead.
This is correct:
document.getElementById('thing').style.left="5px"
But I believe you need to set the position before it can work, e.g.:
document.getElementById('thing').style.position="absolute";
document.getElementById('thing').style.left="5px"

Get Index Question - JQuery [duplicate]

This question already has answers here:
Select element by index (multiple elements of same class)
(4 answers)
Closed 2 years ago.
Possible Duplicate:
Select element by index (multiple elements of same class)
Quick question, I'm targeting all the article elements in my html5 page using:
var articles = $("article");
What I want to do is target one of the article elements using an index only. I can't seem to get it to work, any ideas?:
articles[1].css("display", "none"); // <-- This won't work
The array is returning the DOM element rather than the jQuery object. The .css() function does not exist on the DOM element, so you can wrap it in with the jQuery $ function to create a jQuery object that you can call .css() on.
Try $(articles[1]).css("display", "none");
Demo
Edit: Or even better articles.eq(1).hide();
You can use the .eq() function to target a specific index,
$("article").eq(1).css("display", "none");
According to the jQuery documentation referenced above,
Reduce the set of matched elements to
the one at the specified index.
Try this. This should target the first article
var articles = $('article').eq(0);
articles.css({"display":"none"});
Check this out for more of an explanation but this does exactly what you need.
http://api.jquery.com/eq/

Categories