views:

86

answers:

5

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">?

A: 

Short answer: yes you can.

On the question: How to do it? See documentation on setAttribute method.

anddoutoi
-1, which I'll gladly remove if you edit your answer to tell him how.
Dominic Rodger
Maybe it´s a pointer so he one day learn to ask proper questions.
anddoutoi
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
If you followed the OPs other questions on SO up until now you also should be disillusioned.
anddoutoi
A: 

Using Jquery I believe it is faily easy to do

Here is the related section in Jquery documentation

Aurélien Bottazzini
A: 

Take a look at the DOM.

http://www.javascriptkit.com/javatutors/dom2.shtml

Ben Fransen
+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
this title will be present in source code or not?
metal-gear-solid
can search engine and screen reader can read if we addt horugh this way?
metal-gear-solid
Yes it will be added to the source code.
Guillaume Flandre
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
u mean it will not available in view source of html page in <body>.....</body>
metal-gear-solid
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
Oh yeah, my bad, David's right, if you look up the page's source code, it won't appear.
Guillaume Flandre
@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
@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
+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
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