I want to use classes at a minimum because I already have an ID on the P tag for ASP.

How do I select the second tag exclusively? I could filter by value or content but all the P tags in the list have similar stuff. I also see that I can use the contains() to find the tag based on specifics; but are there other ways to simply select the tag based on it's position?

Thanks for your help.

IC

fretburner
—
2013-06-19T10:53:29Z —
#2

Iconic_creator said:

I want to use classes at a minimum because I already have an ID on the P tag for ASP.

You haven't shown this in your example, but if you're saying that each P element inside the list has an id, then it would be simplest to select using that, e.g. $("#my-p-id");

If you don't have ids that you can select on, you could do something like this: $("li p")[1]; which will select all the paragraphs inside li tags, and give you the second match.

Iconic_creator said:

I want to use classes at a minimum because I already have an ID on the P tag for ASP.

Although it's good to avoid over-using classes and ids by putting them on every other element in the page, don't hold back from using them where it's going to simplify your jQuery selectors.

Pullo
—
2013-06-19T11:35:55Z —
#3

To elaborate on what fretburner says, $("li p")[1] will indeed select all the paragraphs inside li tags, and give you the second match.

However, it will also return a DOM Node, as opposed to the .eq() method which will return a jQuery object (which is useful for chaining).

E.g.

$('li p').eq(1).css('background-color', 'red');

=> Turns the background red.

$('li p')[1].css('background-color', 'red');

=> No effect.

Both have their uses, depending on what it is you are trying to achieve.

The page is an ASP page: So I already have an ID doing things for the Server. So if I put JQuery via the ID, I risk causing a conflict between what the Server is doing on the Tag verses what I need JQuery to do.

But you all make a valid point and I think I should look more into how I can make both Systems use the same tag if at all possible. Maybe there is a way.

Either way, based on your responses I jave more options to move along and see what the best approach is.

Thanks again my friends.

P.S. How do I mark a thread or question as answered?

IC

7stud
—
2013-06-19T17:42:11Z —
#6

I risk causing a conflict between what the Server is doing on the Tag verses what I need JQuery to do.

I don't know anything about ASP, but I'll still say, "That's impossible."

Pullo
—
2013-06-19T18:16:38Z —
#7

Hi there,

Iconic_creator said:

I think I should look more into how I can make both Systems use the same tag if at all possible. Maybe there is a way.

I don't see any reason that this would not be possible as jQuery and ASP are two completely unrelated technologies.

Iconic_creator said:

P.S. How do I mark a thread or question as answered?

You can't, I'm afraid.I asked the same thing a while back. The rationale is that it's always possible that someone with a better answer turns up and adds to the discussion.

Iconic_creator
—
2013-06-23T08:07:08Z —
#8

7stud said:

I don't know anything about ASP, but I'll still say, "That's impossible."

It is not impossible but it depends on your project: There are hundreds of books that will tell you the pain it can take to do some simple things. Inf act, this is the biggest selling points of MVC as it outputs pure HTML and sits very well with JQuery.

Pullo
—
2013-06-23T12:23:43Z —
#9

Just out of interest, could you give us an example of what you mean.

Iconic_creator
—
2013-06-23T18:56:40Z —
#10

Pullo said:

Just out of interest, could you give us an example of what you mean.

Sure, no problem:

The following code is a section of my primary form: I find it better to put all of my form elements in a list, this way I can align or place them in precise position as if they were in a table:

[I][B]
Will look like this after it is rendered by the server:[/B][/I]

<span>What is your name</span>

I decided to use paragraph tags and control them from the server by adding the server attribute and an ID.
Another thing I did for the sake of [I][B]performance[/B][/I] is to remove the ID of any tags that is not used immediately by the server:
[B][I]Something like this:[/I][/B]
[highlight="C#"]
<asp:Label ID="lblThisIsMyId" runat="server" Text="Whats your name"></asp:Label>

Will look like this:

<span>Whats your name"></span>

This may not same like much but it reduces ViewState and provides a more cleaner HTML. Especially for pages where there are hundreds of tags with ID.

Now how does this affect JQuery? It doesn't, however it does affect my approach and design.The problems I had is that when ASP.NET is using a specific tag, then I have to work around it so that JQuery has access to the same tags.

Example: I can no-longer hide the ID's. But the biggest hurdle is with the error messages. If the server is hiding the error messages on Load, then JQuery cannot access those tags. So instead of hiding the error messages on the server,I simply had to add the CSS dynamically by the server. But to do this I still need an ID on the p tags because that's the only way the server can interact with them (I do not know another way).

[I]
This line removes the ID if the tag is not in use of if there are no error messages:[/I]
[highlight="C#"]
lblUserName.ID = (Validated.IsValidUsername ? "" : "lblUserName");

The result of all of this is that I have a cleaner more usable HTML.

I am not saying ASP.NET does not work with JQuery, I am saying ASP.NET is not like PHP where tags can love each other side by side. ASP.NET needs an ID for everything and if those ID are doing other things, you may need to add JQuery directly to the tags or add a class.

Lastly, the plan to switch to ASP.NET MVC which is more cleaner, I am a purist I hate a page fill with bunch of span tags. I also cannot stand anything dynamic generated (html). With MVC you write your own HTML, so nothing is really generated. You have control of what tags goes where.

Thanks again!

Pullo
—
2013-06-27T08:23:34Z —
#11

Hi there,

Thanks for taking the time to reply.I think we are talking about slightly different things.I thought you meant that by including a class or id attribute on a tag (e.g. <span class="whatever">), you could cause some kind of conflict between ASP and jQuery.However, having read your reply, this does not seem to be the case.