james bond
james bond

Reputation: 117

display alert dont show

My js error,

how to display "1 months" in alert

ori html :

<div class="left" style="height:15px; width:128px; overflow:hidden;">1 months</div>

my js code :

var div = document.querySelector('[class="left"][style="height:15px; width:128px; overflow:hidden;"]');

alert( div[0].innerHTML );

Upvotes: 0

Views: 132

Answers (4)

jsjunkie
jsjunkie

Reputation: 559

just use alert( div.innerHTML ); instead of alert( div[0].innerHTML );

Also make sure that you call this after your div is rendered i.e like

<!DOCTYPE html>
<html>
<body>
<div class="left" style="height:15px; width:128px; overflow:hidden;">1 months</div>
<script>
var div = document.querySelector('[class="left"][style="height:15px; width:128px; overflow:hidden;"]');

alert( div.innerHTML );
</script>
</body>
</html>

Upvotes: 0

Grundy
Grundy

Reputation: 13381

try change your code like this

var div = document.querySelector('[class="left"][style="height:15px; width:128px; overflow:hidden;"]');

alert( div.innerHTML );

or

var div = document.querySelectorAll('[class="left"][style="height:15px; width:128px; overflow:hidden;"]');

alert( div[0].innerHTML );

Upvotes: 1

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Try this

alert(document.getElementsByClassName('left').innerHTML);

Upvotes: 0

Smeegs
Smeegs

Reputation: 9224

I think the problem is with your query selector. To select the div by class name you can go with

var div = document.querySelector('.left');

If you have more than one element with the same class, I would recommend setting an id for the div.

<div id='IDNAME' class="left" style="height:15px; width:128px; overflow:hidden;">1 months</div>

And you can query it like so:

var div = document.querySelector('#IDNAME');

Upvotes: 0

Related Questions