473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to select the first tag out of many tags?

e.g.
<div class="main">

<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>

</div>

how to select the first `p` tag only?

Dec 30 '06 #1
13 14507
On 2006-12-30, howa <ho******@gmail.comwrote:
e.g.
<div class="main">

<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>

</div>

how to select the first `p` tag only?
I don't think there is a way other than e.g. giving it a class and using
a class selector.
Dec 30 '06 #2
Hi Howa and Ben,

Right, the easy way to do this is to use the following style selector:

div.main p:first-child { ... }

This tells the browser to find all P tags that are immediate children
of div.main and then of these select the first child.

The problem with this however is that IE6 won't display it as it
doesn't understand the child selector or the first-child pseudo class
so you are a bit scupperred there, however IE7 understands both without
any problem so the definition above will work fine.

If you want IE6 support you can do it with a cheeky bit of javascript
in your onload event, basically adding a class to the first p child of
the main div.

Thus:

function AddChildSelector() {

var divlist = document.getElementsByTagName("div"); // if you used an
ID you can use GetElementsById() instead which would be quicker

for (i=0; i<divlist.length; i++) {
if (divlist[i].className == "main") {
var plist = divlist[i].getElementsByTagName("p");

if (plist.length 1) {
plist[0].className = "first_child";
}
}
}
}

Will do the job for you if you add the following selection and rule to
the CSS

div.main p.first_child { ... }

Bear in mind I've just knocked this up quickly - really you would want
a browser check to go in there in order to only do it on IE6 or other
browsers where they have no first child selector. This method is also
dependent on javascript and is completely brittle in that it will only
work for the HTML you've given - but it is easily abstracted to make it
work in more generic cases.

Now IE7 is is about the same as FF in terms of % of users I think we'll
be doing more of this for the next year as IE6 phases out.

I'd either not bother with the style for IE6 users if it is purely
cosmetic which will give you a good degrade or have a library of
functions like this that are dropped in when the server finds an IE6
user agent.

Cheers
AndrewF

---

www.xmlinfinity.com - Content management where the possibilities are
endless.

Ben C wrote:
On 2006-12-30, howa <ho******@gmail.comwrote:
e.g.
<div class="main">

<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>

</div>

how to select the first `p` tag only?

I don't think there is a way other than e.g. giving it a class and using
a class selector.
Dec 30 '06 #3
AndrewF wrote:
Hi Howa and Ben,

Right, the easy way to do this is to use the following style selector:

div.main p:first-child { ... }

This tells the browser to find all P tags that are immediate children
of div.main and then of these select the first child.
If one of them *is* the first child of the DIV. To clarify, if a DIV has
children that are Ps but the DIVs first child isn't a P, then there's no
match. To use the W3C's example,

<PThe last P before the note.
<DIV class="note">
<H2>Note</H2>
<PThe first P inside the note.
</DIV>

Here, div p:first-child doesn't match either P.
Dec 30 '06 #4
howa schrieb:
<div class="main">

<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>
<p>ssssss</p>

</div>

how to select the first `p` tag only?
p {
/* rules for first p */
}
p+p {
/* overwrite rules for other p elements */
}
--
Johannes Koch
Spem in alium nunquam habui praeter in te, Deus Israel.
(Thomas Tallis, 40-part motet)
Dec 30 '06 #5
Scripsit Johannes Koch:
p {
/* rules for first p */
}
p+p {
/* overwrite rules for other p elements */
}
Nice, but it doesn't work if there's any element between p elements. If you
have, say,

<p>...</p>
<p>...</p>
<hr>
<p>...</p>
<p>...</p>

then the rules for "first p" will apply to the p element after the hr
element, too.

Moreover, not all rules can be overridden. If you set, say, font-family to
some value, there is no way to undo that setting generally, to say "use the
font-family value you would use by default". You can only set it to some
particular value. Being careful, you might be able to undo the settings for
p _in a specific context_, of course.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Dec 31 '06 #6
Jukka K. Korpela wrote:
Scripsit Johannes Koch:
>p {
/* rules for first p */
}
p+p {
/* overwrite rules for other p elements */
}

Nice, but it doesn't work if there's any element between p elements. If
you have, say,

<p>...</p>
<p>...</p>
<hr>
<p>...</p>
<p>...</p>

then the rules for "first p" will apply to the p element after the hr
element, too.
How about *+p?
Dec 31 '06 #7
Ben - good point, I completely forgot about that quirk of
first-child... that it selects the p if it *is* the first child of the
div rather than the first child of the dic which is a P tag - which
rally should be the way it works in my opinion... would make things a
lot more flexible.

I should point out that the javascript function I wrote above exhibits
this behaviour, so feel free to pinch and adapth if you want.

Of course this is all academic because none of the solutions given
[using sibling selectors etc] will work in IE6 so you need to come up
with something that degrades.

Howa, if you know for absolute certain that a <pwill always be the
first child of your div.main then you don't have an issue - if it can
vary then you are in a world of pain - just add the class attribute and
be done with it.

Of course, if you only have a certain combination of alternatives from
the one given then you can always have different selectors that do the
same thing:

eg: if you end up with this say:

<div class="main">
<h1>Some heading</h1>
<p>sssss</p>
<p>sssss</p>
<p>sssss</p>
<p>sssss</p>
</div>

then "div p:first-child {...}" won't work

However if you added a variation on what Johannes suggests so you have

div.main h1 + p { ... }

then this will work and select the p which is a sibling of the h1 which
are children of the div.main - if you have a limited number of cases
which you can guarantee will always be the case [which in using
semantic HTML you probably can] then you haven't got an issue - though
as Ben says, this is quite brittle in that an extra tag in the wrong
place will break it.

This does bring us full circle again however, which is that none of
this has any support in IE6 so if having the first child selected is of
vital importance and is crucial to your design you'll need another way
of doing it and the only relaible way is to add an id or class
attribute.

I've mentioned this elsewhere recently but it would be good if CSS used
selection methods more akin to XPath [considering the node trees work
the same way] as you'd get a lot more out of it - that's what happens
when there is more focus on the style part of the spec than the
selection part!

Cheers
AndrewF

www.xmlinfinity.com - content management where the possibilites are
endless.
Harlan Messinger wrote:
Jukka K. Korpela wrote:
Scripsit Johannes Koch:
p {
/* rules for first p */
}
p+p {
/* overwrite rules for other p elements */
}
Nice, but it doesn't work if there's any element between p elements. If
you have, say,

<p>...</p>
<p>...</p>
<hr>
<p>...</p>
<p>...</p>

then the rules for "first p" will apply to the p element after the hr
element, too.

How about *+p?
Dec 31 '06 #8
AndrewF wrote:
Ben - good point, I completely forgot about that quirk of
first-child... that it selects the p if it *is* the first child of the
div rather than the first child of the dic which is a P tag - which
rally should be the way it works in my opinion... would make things a
lot more flexible.
I kind of agree--it seems to me that you'd want "the first X" that's a
child of a given element much more often then you'd want "the first
child, but only if it's an X".
Dec 31 '06 #9
Scripsit Harlan Messinger:
How about *+p?
Yes, how about it? You quoted my entire message, so it is impossible to say
what your question relates to. If taken as addressing the question in the
Subject line, it is plain wrong. So do you suggest that it addresses the
issue of constructing a selctor that matches any p element that is _not_
"the first tag out of many tags".

Please be more explicit in future, and (as part of that, too) trim your
quotations so that it is apparent what you are commenting on.

If you write, say,

p { font-weight: bold; }
* + p { font-weight: normal; }

then you indeed end up with bolding each p element that is the first element
in a sequence of elements, in a sense. In any sequence of paragraphs, only
the first one would be styled, even if there are other elements, such as
headings, tables, or div elements between the paragraphs. But if a p element
is nested inside another element, such as a div element, then the first one
of p elements so nested in an element would be styled.

Of course, on browsers like IE 6 (or IE 7 in "quirks" mode!), the rules
would reduce to p { font-weight: bold; }, thereby styling _all_ elements
the way you wanted to do to "first" p elements only.

Moreover, how often would this really be useful? The practical occasions
where I have wanted to style "first" p elements differently, it has been a
matter of adjusting vertical margins suitably (e.g., not too much spacing
between a heading and a subsequent paragraph) or setting text-align (first
line indent) for a paragraph when it follows another paragraph (when I have
set vertical margin between them to zero). So it's not an abstract question
of styling "the first tag out of many tags" but something more specific.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Jan 2 '07 #10
Jukka K. Korpela wrote:
Scripsit Harlan Messinger:
>How about *+p?

Yes, how about it? You quoted my entire message, so it is impossible to
say what your question relates to. If taken as addressing the question
in the Subject line, it is plain wrong. So do you suggest that it
addresses the issue of constructing a selctor that matches any p element
that is _not_ "the first tag out of many tags".

Please be more explicit in future, and (as part of that, too) trim your
quotations so that it is apparent what you are commenting on.
Give me a friggin' break. Your "entire message" was a single assertion,
"Nice, but it doesn't work if there's any element between p elements"
followed by a short illustration of that single assertion, and I was
responding to the assertion. Very simple. Please stop inventing reasons
to be obnoxious in the future.
Jan 2 '07 #11
Scripsit Harlan Messinger:
>Please be more explicit in future, and (as part of that, too) trim
your quotations so that it is apparent what you are commenting on.

Give me a friggin' break. Your "entire message" was a single
assertion, "Nice, but it doesn't work if there's any element between
p elements"
Then you should have quoted that very statement only.

Among the annoying fullquoters, you're one of the few who couldn't be
killfiled without losing interesting content, so I hope you get the hint.

Naturally, I would have sent this advice by personal email only, if you
didn't use a forged address.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Jan 2 '07 #12
Jukka K. Korpela wrote:
Scripsit Harlan Messinger:
>>Please be more explicit in future, and (as part of that, too) trim
your quotations so that it is apparent what you are commenting on.

Give me a friggin' break. Your "entire message" was a single
assertion, "Nice, but it doesn't work if there's any element between
p elements"

Then you should have quoted that very statement only.

Among the annoying fullquoters, you're one of the few who couldn't be
killfiled without losing interesting content, so I hope you get the hint.
It's not as though I left pages and pages of irrelevant discussion. What
I left was your very brief illustration that was intimately tied in with
the point that you made, AND which served just as well to illustrate MY
proposed solution. Did you think it was necessary for me to delete your
example, only to write it all over again to illustrate MY point?

Anyway, go ahead and killfile me. Who do you think you are?
Naturally, I would have sent this advice by personal email only, if you
didn't use a forged address.
It isn't forged (look up "forge" in the dictionary), and it contains a
very simple instruction for altering it to work that you apparently
didn't feel competent to follow.
Jan 2 '07 #13
Scripsit Harlan Messinger:
>Naturally, I would have sent this advice by personal email only, if
you didn't use a forged address.

It isn't forged (look up "forge" in the dictionary),
It is. I did. You intentionally created something that looks like an email
address but isn't.

By your request, I will do my best to avoid your postings. Please continue
using the same forged address as long as you continue misbehaving.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Jan 2 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike R | last post by:
Hi, I have a table called opportunity, included within this table are a opclosed (date) , a companyid (integer) and some other columns. There are many opportunity records per companyid . I am...
0
by: isel77 | last post by:
Hi, I get a null reference exception while accessing the first row returned by a Select in a DataTable of a DataSet (typed) stored in cache ( datatable.Select( "FieldX=1" ) ). The error occurs...
2
by: Andrew Lias | last post by:
Let us say that I have a table with two sets of values as such: Item Extension --- ---- 100023 1 100025 1 100025 2 100028 1 100029 1 100029 2
7
by: skeddy | last post by:
In a nutshell, I'm trying to dynamically create a select box with ResultSet code in vbscript and then need to be able to access the value of that select box later with a Save button. I've got...
5
tolkienarda
by: tolkienarda | last post by:
hi all i need to select the first fifteen words of an article stored in a mysql database. so i have the basic idea laid out but the exact syntax seems a bit confusing. i have a book here on...
0
by: =?Utf-8?B?Q2FibGVHdXk=?= | last post by:
If I have an unsorted listview I know I can select the first Item by ListView.Items.Selected = true. How do I select the first item in a sorted listview? Thanks. CableGuy
1
jamesd0142
by: jamesd0142 | last post by:
Hi i want to select the first row only in my table... i have this: declare @abc varchar(2000) set @abc = (select Sql_Table_Name from temptable10) print @abc how can i edit it to only...
1
by: Cyprus106 | last post by:
So I've got about a million records that I'm sifting through with a SELECT statement. Right now I've been saying SELECT * FROM MYTABLE WHERE MYFIELD='GUIDE'... But I only want the first record it...
2
by: shapper | last post by:
Hello, I have the following LINQ: List<Tagtags = (from t in database.Tags select t).ToList(); I need to restrict my results and for that I have two parameters: StartWidth and N
5
by: eko99312 | last post by:
Pardon me if this question already been asked before. I tried to sort a query to meet my desire. Here is the example: Date Customer Supplier Price 31-Oct-09 Charlie Australia $ 100...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.