9

I have a simple code

<div class="content"></div>

I want to echo something inside the div tag using javascript to show this way

<div class="content" something></div>

I need it using javascript because I want to use a function to echo something if the screen is wider than 960px I used this answer for the second part Do something if screen width is less than 960 px

Thanks guy in advance.

Community
  • 1
  • 1
user3358086
  • 95
  • 1
  • 1
  • 6

3 Answers3

8

That called attribute, to set attribute you could use the methods below.

Pure JS setAttribute() method :

element.setAttribute('name','value');

jQuery attr() method :

$(element).attr('name','value');

Example :

document.querySelector('.content').setAttribute('something',''); //Pure JS
//Or
$('.content').attr('something',''); //jQuery

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

With jQuery:

$('.content').attr('something','');
SLePort
  • 15,211
  • 3
  • 34
  • 44
1

If you want to add the attribute if window width is greater then 960

if ( window.innerWidth > 960 ) {

    document.querySelector('.content').setAttribute('something','')

}
Filipe
  • 1,788
  • 2
  • 13
  • 18