0

i have this code:

 <a href='google.com'  title=' '> <script>exam='y';</script> </a>

I want to add this script <script>exam='y';</script> inside title tag, like this:

 <a href='google.com'  title=' <script>exam='y';</script> '> <script>exam='y';</script> </a>

But it does not work. Hope for your helps.

2
  • What are you expecting, what are you seeing? The first obvious answer would be to use " quotes for the title string Commented Jun 16, 2013 at 6:24
  • 1
    I think you want to change the title of your page dynamically... Do specify EXACTLY what it is that you want to do... Commented Jun 16, 2013 at 6:25

1 Answer 1

2

Put your javascript separate in script tag and then point to your element attribute to set it

<a id="myId" href='google.com' title=''> my link </a>


window.onload = function () {
    document.getElementById("myId").setAttribute("title", "some title");
}

fiddle: http://jsfiddle.net/djwave28/rHddx/1/

If you set your title dynamically, you do not need the attribute in the tag. It will be set by javascript, as thfollowing fiddle will show you.

http://jsfiddle.net/djwave28/rHddx/2/

Sign up to request clarification or add additional context in comments.

4 Comments

If the script is placed after the element it looks for, you don't need to wait for the page load.
Also, it must be noted that there can only be one onload handler defined this way. If you need two of them, use addEventListener (+attachHandler if you need to support IE<9 - or use jQuery).
@Jan - yes, correct. As soon as an element is created in the DOM, you can manipulate it.
@Jan - Again, you are correct. That situation will however be scarce. Personally I would prefer just one as a constructor. That is more a matter of preference. Thanks for adding some thoughts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.