0

I want to add id="draggable" attributes to all tags.

For example -

<img src="http://sample.jpg"> should change to <img src="http://sample.jpg" id="draggable" >

<p>abcd</p> should change to <p id="draggable">abcd</p>

This should apply to all tags except <HTML><HEAD><script><style>

Any suggestion?

3
  • 8
    Putting the same id for a bunch of elements on the same page is not a sound plan. Ids are supposed to be unique. Commented Jan 26, 2015 at 6:13
  • 1
    adding same id to different elements is not a good approach...go for class.. Commented Jan 26, 2015 at 6:13
  • Yes got it ..thank you so much. Now I am using class attribute Commented Jan 26, 2015 at 6:20

3 Answers 3

1

Use class instead of Ids

var elems = document.body.getElementsByTagName("*");
elems.setAttribute("class","draggable");
Sign up to request clarification or add additional context in comments.

Comments

0

ID(s): Each element should only have one ID Each page can only have one element with that ID CLass(s): You can use mutliple classes on one element. You can use the same class on multiple elements

1 Comment

Yes agreed - I am using class attribute now
0

Adding same id to all the HTML elements is not a good way. Better to have same class name. Dynamically, if you want to add same class to all the elements, you can do something like below

$('#parent').find('*').addClass('draggable');

http://jsfiddle.net/663m8qyd/

<div id='parent'>
    <p>ABC</p>
    <img src='http://www.w3schools.com/tags/smiley.gif' alt="Smiley face"/>
    <div id='child'>
        <div id='child1'>test1</div>
        <div id='child2'>test2</div>
    </div>
</div>

To add draggable class to all the elements inside parent div, find each element and add respective class. Hope that helps!

2 Comments

Its not tagged jQuery
@RahulB...Agreed!..Although by that way.. regex tag doesn't seem logical too.. ;)

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.