Selecting elements based on absence of class-attribute in HTML DOM - javascript

Using the HTML DOM, I would like to select almost all td tags except for those td tags that have a class-attribute of "xyz".
With document.selectElementsByTagname["td"] I can get all the td-elements. However, I don't want all but only those where the class-attribute != "xyz".
Since there are no predicates in html DOM, I currently don't see a way to achieve this. Is there still a way to do it?

You can use querySelectorAll with :not() pseudo class selector.
document.querySelectorAll("td:not(.xyz)")
Array.from(document.querySelectorAll("td:not(.xyz)")).forEach(function(e) {
e.style.color = "red";
})
<table>
<tr>
<td class="xyz">a</td>
<td class="xyz">a</td>
<td>a</td>
<td class="xyz">a</td>
<td>a</td>
<td class="xyz">a</td>
</tr>
</table>

You do like this:
document.querySelector("td:not(.xyz)")

Related

unable to get hidden field value using parent 's class name

My html code is-
<tr class="selected">
<td id="participantID">XXXXX1234</td>
<input type="hidden" value="000001234" id="taxID">
<td id="fullName">Y, X</td>
</tr>
Here, I want to get hidden field value. I can not use ID of hidden field to get its value because there are multiple rows which can contain hidden field with same ID as "taxID". I want to get this value using <tr> class name.
i.e. selected.
I am using below code to get its value but it is giving me 'undefined' value.
var x = document.getElementsByClassName("selected")[0];
var y = x.getElementsByTagName("input")[0];
alert(y.value);
Alert statement shows undefined value. Am I missing something over here?
First, you cannot have multiple elements in a document with identical id values. That will have to be altered and that alone may solve your problem.
Second, your HTML is invalid. The input must be inside of a td.
Next, there is no reason to use getElementsByClassName() or getElementsByTagName() when you are looking for just one element - it's wasteful because you wind up searching the entire document when you are only interested in one item.
Also, both of those methods return "live" node lists which require re-scanning the entire document every time their results are referenced. The use cases for that are limited.
Instead use .querySelector() when you want to find just one item based on any valid CSS selector and .querySelectorAll() when you want to find a set of matching elements.
Assuming these things are corrected, you can do this:
var x = document.querySelector(".selected td input[type=hidden]");
alert(x.value);
<table>
<tr class="selected">
<td id="participantID">XXXXX1234
<input type="hidden" value="000001234" id="taxID">
</td>
<td id="fullName">Y, X</td>
</tr>
</table>
You need to have a table be the parent of a tr, then the DOM lookup will properly work. Also as noted by #Rory McCrossan you will want to wrap td tag around your input element:
var x = document.getElementsByClassName("selected")[0];
var y = x.getElementsByTagName("input")[0];
alert(y.value);
<table>
<tr class="selected">
<td id="participantID">XXXXX1234</td>
<td><input type="hidden" value="000001234" id="taxID" /></td>
<td id="fullName">Y, X</td>
</tr>
</table>
(Posted solution on behalf of the OP).
After removing ID of hidden field, it is working fine. Edited code is:
<tr class="selected">
<td id="participantID">XXXXX1234</td>
<input type="hidden" value="000001234" id="taxID">
<td id="fullName">Y, X</td>
</tr>

Find element by class and custom attribute value (then update it)

Using jQuery I want to add some HTML to a specific <td> whose attribute matches the value I have.
For example I have a value '2015-02-01', I want to find/search the <td> which had the attribute 'data-date="2015-02-01"' and append HTML to the matched value, which in this example is <td>1</td>
<div class="fc-content-skeleton">
<table>
<thead>
<tr>
<td data-date="2015-02-01" class="fc-day-number">1</td>
<td data-date="2015-02-02" class="fc-day-number">2</td>
<td data-date="2015-02-03" class="fc-day-number">3</td>
<td data-date="2015-02-04" class="fc-day-number">4</td>
<td data-date="2015-02-05" class="fc-day-number">5</td>
<td data-date="2015-02-06" class="fc-day-number">6</td>
<td data-date="2015-02-07" class="fc-day-number">7</td>
</tr>
</thead>
</table>
</div>
I dont have any sample code as I am stuck really on how to approach this.
// This only gives me the value of the attribute
$this.parent().find('.fc-day-number').attr('data-date');
Just use attribute selector whose syntax is [attributeName[=attributeValue]]
var value = "2015-02-01";
$('[data-date="' + value + '"]').append('html here');
Note, that you can apply the above selector in find, closest and other such jQuery methods that accept a CSS selector.
Use jQuery Attribute Equals selector
then use jQuery html() to add content
or use jQuery append() to append content
here is the html() fiddle
I think is that you want :
var date = "2015-02-03";
$("td[data-date='" + date + "']").append(date);
Solution on this url : http://jsfiddle.net/g09jvfxy/1/

Why can I get the value attribute from a table with jQuery and is it safe to use

So I figured I would try to add the 'value' attribute to a TD tag because I need to store a value in a table and really kind of wanted it to be not so obvious, to my amazement, using jQuery I was able to retrieve this value.
My question is why am I able to get this value when it isn't a valid attribute and since I can, would it be safe to use.
HTML
<table id="tblTest">
<tr>
<td value="0">Value is zero</td>
</tr>
<tr>
<td value="1">Value is one</td>
</tr>
</table>
Javascript:
$('#tblTest').on('click','tr', function(){
alert($(this).children(':first').attr('value'));
});
I have created a fiddle for it http://jsfiddle.net/r2Lqp/
If I understand the question, my answer is this: you can add any attributes for personal use if they are not reserved in standards HTML.

Removing some text data in HTML file

I am working on visualforce pages. below is given the part of HTML file code that has been generated after executing the apex code.
<table class="detailList" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr></tr>
<tr>
<td class="labelCol"></td>
<td class="dataCol col02"> userName </td>
<td class="labelCol"></td> <td class="dataCol"></td>
</tr>
<tr>
<td class="labelCol"></td>
<td class="dataCol col02"></td>
<td class="labelCol"></td>
<td class="dataCol"></td>
</tr>
</table>
I want to remove the userName anchor tag from this page which is coded in line# 6 whose class Name is "dataCol col02", and there is another anchor tag with the same class name "dataCol col02" at line# 11. keep it in mind that this html is generated by executing an APEX code. Kindly guide me how could i remove the anchor tag at line#6 only..
You can use find, first and remove methods.
$('.dataCol.col02').first().find('a').remove();
In case that you want to remove the userName textNode:
$('.dataCol.col02').first().contents().filter(function () {
return this.nodeType === 3;
}).remove();
Removing all the contents:
$('.dataCol.col02').first().empty();
Use this
$(function(){
$(".dataCol.col02:first a").remove();
});
Demo
You could do something like:
var anchor = document.getElementsByClassName("col02")[0] //select first matching 'col02'
.getElementsByTagName("a")[0] //select first matching <a>
anchor.parentNode.remove(anchor)
You can see it running here: jsfiddle
This assumes of course you only ever want to remove from the first instance of something with class='col02', so is not hugely robust. I imagine the fact it's generated means you can't put in more helpful class/id attributes?
On the flipside unlike the other answers it doesn't depend on jquery : )
Try this -
$('td.dataCol.col02').eq(0).find('a').remove();
or if you would like to empty that td -
$('td.dataCol.col02').eq(0).empty();
$("table .dataCol.col02:first a").remove();
Try this:
$("tr:eq(1) > td:eq(1)").remove()
Do this >>
$(".col02:first > a").remove();
Example Fiddle

Rails 3 jQuery : How to alert id of td

I have a 3x3 table of td's each with id's (id='a1'...id='c3'). I'd like to be able to click on any of the 9 td's and to alert the id of that td.
Here is my Coffeescript (in the asset pipeline)
$(document).ready ->
$("td").click ->
alert(#I would like to alert the id of whichever of the 9 td cell's have been clicked on)
Here's my index.html.erb
<table>
<tbody>
<tr>
<td id='a1'>A1</td>
<td id='b1'>B1</td>
<td id='c1'>C1</td>
</tr>
<tr>
<td id='a2'>A2</td>
<td id='b2'>B2</td>
<td id='c2'>C2</td>
</tr>
<tr>
<td id='a3'>A3</td>
<td id='b3'>B3</td>
<td id='c3'>C3</td>
</tr>
</tbody>
</table>
I'm horrible at JS so any help is appreciated!
$('td').click: (e)->
alert $(#).id
Same in CoffeeScript
Also, storing user data with html element is very easy with data-* attributes, for example:
<td data-id="42"></td>
And getting this id is easy with jQuery data method like follows:
var id = $('td').data('id');
First off, using jQuery on yields better performance than attaching a click handler to each td, especially if you have lots of tds:
$('table').on 'click', 'td', (event) ->
# event.currentTarget is the td which was clicked
alert event.currentTarget.id
event.currentTarget will be a DOM element object, and so every attribute will be available as a property of the object. The other answers referring to $(this).id are wrong, since $(this) (or $(event.currentTarget)) is a jQuery object, and as such attributes are available with the attr method: $(this).attr('id').

Categories

Resources