Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an attribute to the div tag

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.

like image 479
user3358086 Avatar asked Dec 19 '16 10:12

user3358086


2 Answers

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.

like image 90
Zakaria Acharki Avatar answered Sep 17 '22 17:09

Zakaria Acharki


With jQuery:

$('.content').attr('something','');
like image 33
SLePort Avatar answered Sep 18 '22 17:09

SLePort