Marina
Marina

Reputation: 3322

When using id's by jquery to do something, can the id's come from any kind of tag?

I am new to JQuery, and I am attempting writing code using PHP, HTML, and JQuery.

I want to replace all of the <?php echo $var; ?> in my HTML with tags that have id's, instead. For example, I want to have something like <div id="name"></div> which would then use a $("#name") to have it display something in that div field.

My questions: Does it have to be a <div> tag with that id when I call the #name in jquery? Or can it be in any tag and still have the same effect? --Would this work as well: <a href='alink'><div id="name"></div></a>. What if I want to put tags with id's to replace the alink--What tags would be good for that?

Any help appreciated.

Upvotes: 1

Views: 53

Answers (3)

Kyle
Kyle

Reputation: 4449

You can add an id to any field. However, you can only use that particular id ONCE per page.

Ex.

<div id="name"></div>
<p id="name"></div>
<a id="name"></a>

The above is invalid and will cause problems if you use $('#name') in your jQuery. If you want to alter multiple things, like shown above, you are better off using a class and a different kind of selector:

<div class="name"></div>
<p class="name"></div>
<a class="name"></a>

$('.name')

This will select both of the elements.

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116100

It can be any tag (=element), not just divs.

Upvotes: 1

jondavidjohn
jondavidjohn

Reputation: 62392

An id can be an attribute of any html tag (element).

It also must be completely unique to the html document at any given time.

<?php echo $var; ?> however is not html at all, it is PHP which is something separate all together.

Upvotes: 1

Related Questions