For some reason I can't edit XHTML source. Can we add any attributes to any XHTML Tag through Javascript, for example title in <a title="text">
?
views:
86answers:
5
Q:
Can we add any attributes to any XHTML Tag through Javascript ? for example title in <a title="text>
A:
Short answer: yes you can.
On the question: How to do it? See documentation on setAttribute method.
anddoutoi
2009-11-20 10:15:17
-1, which I'll gladly remove if you edit your answer to tell him how.
Dominic Rodger
2009-11-20 10:15:58
Maybe it´s a pointer so he one day learn to ask proper questions.
anddoutoi
2009-11-20 10:18:31
What's wrong with his question as he originally asked it? Yes, technically the question stated "can you", when he meant "how do you", but it's hardly difficult to work out what he meant.
Dominic Rodger
2009-11-20 11:11:39
If you followed the OPs other questions on SO up until now you also should be disillusioned.
anddoutoi
2009-11-20 11:39:47
+2
A:
With jQuery there's the attr() methiod that allows you to do that: http://docs.jquery.com/Attributes/attr
HTML:
<a href="#">link</a>
Javascript (jQuery):
$("a").attr("title","my title");
Guillaume Flandre
2009-11-20 10:15:58
can search engine and screen reader can read if we addt horugh this way?
metal-gear-solid
2009-11-20 10:23:19
It will be in the JavaScript source. It won't be in the HTML source.Search engines do not execute JavaScript. Screen readers read whatever is on the page — so it depends on the browser supporting JS and having it turned on (a significant number of screen reader users disable JS as a lot of badly written JS causes them problems).
David Dorward
2009-11-20 10:27:32
u mean it will not available in view source of html page in <body>.....</body>
metal-gear-solid
2009-11-20 10:32:22
my problem is i have 100 images on page and i want to add different title to each image.How can i do this?
metal-gear-solid
2009-11-20 10:37:04
Oh yeah, my bad, David's right, if you look up the page's source code, it won't appear.
Guillaume Flandre
2009-11-20 10:38:12
@Jitendra: If it's just a matter of displaying a tooltip over the link, you can use this javascript solution; if it's for SEO purposes, then you should edit the source code directly.
Guillaume Flandre
2009-11-20 10:40:23
@Jitendra: You should add the appropriate attribute in the server-side code that outputs HTML. Or, if you've already generated HTML and want to modify it before sending it to the browser, use a DOM parser and loop through the `a` links to add attributes.<http://code.google.com/p/phpquery/> PHP implementation of jQuery<http://www.codeplex.com/htmlagilitypack> HTML Agility Pack for .NET
Krof Drakula
2009-11-20 11:10:43
+3
A:
Yes. There is no need to break out the jQuery for something so very simple. Also avoid setAttribute
in general as there are problems with it in IE (not for the title
attribute, but for many others).
Instead just use the perfectly normal DOM Level 1 HTML property:
link.title= 'text';
So eg. if the link you wanted to change was <a href="..." id="foo">
:
document.getElementById('foo').title= 'text';
bobince
2009-11-20 13:18:56
Thank you for that, much as I love a good api, its less code and way less overhead (in this instance) to just do the raw js.
OhkaBaka
2009-11-20 13:55:45