4

What I've tried:

function addAttribute(){
     document.getElementById('myid')... 
};
window.onload = addAttribute;

How can I add add the attribute to my element with id="myid" ?

3 Answers 3

5
document.getElementById('telheaderid').yourattribute = "your_value";

For instance

document.getElementById('telheaderid').value = "your_value";

Using jQuery:

$('#telheaderid').attr('value', 'your_value');

EDIT:
Focus is the event that fires up when an element get focused or for instance when we click on the textarea it highlights thats the time.

Using jQuery:

$('#telheaderid').focus(function() {
   $(this).val('');
   // run any code when the textarea get focused
});

Using plain javascript:

document.getElementById('telheaderid').addEventListener('focus', function() {
   this.value = "";
});
Sign up to request clarification or add additional context in comments.

10 Comments

Try to add this code in javascript console and then try if it works then you must be running this code somewhere wrong
Or give me the link or access if it's admin side I'll add it
It works on the console but not if i add it to the header.php, what am i doing wrong? I can see the code there..
Add this script tag in footer.php just after the body tag ends instead of header.php and try again man! This time I think it would work. And don't forget to accept the answer!
A second method would be to wrap your code in $(document).ready(function(){ /* your code here */ });
|
1

Use this:

document.getElementById('telheaderid').setAttribute('class','YourAttribute')

2 Comments

But i already can set the class, i need to add an attribute like "OnClick" for example so then i add another function, onClick and i can make it clear the value.
This works with any Attribute. You can replace the 'class' with e.g. 'onclick','name','style','onblur' etc.
1

The W3C standard way:

function functionAddAttribute(){
     document.getElementById('telheaderid').setAttribute('attributeName', 'attributeValue');
};
window.onload = functionAddAttribute;

for IE:

function functionAddAttribute(){
     document.getElementById('telheaderid').attributeName = 'attributeValue';
};
window.onload = functionAddAttribute;

Enjoy your code!

6 Comments

Thank you for helping me out but its not working i really dont know why. i.imgur.com/Ky5ePUv.png
you try this: jsfiddle.net/4b4U9 there is a problem with window.onload function
Thank you, can you try initializing it with a default value then clearing it with the javascript please?
Here is the link: jsfiddle.net/4b4U9/2 you can use setAttribute o removeAttribute
Why not? You see the alert box? The attribute size has blank, or not? Which browser use? Which version?
|

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.