Javascript getElement methods doesn't work

(SMHassanAlavi) #1

I want to find some element in my web page with id = “ember1124”, But my code doesn’t work.
for Example:

<tr id="ember1124" class="ember-view topic-list-item category-ورزشی" data-topic-id="27">  

You all can see the id=“ember1124” but
return nothing. or in some cases it returns wrong number with .length method.
I also used getElementsByClassName and methods like this but they don’t work in my case.
works fine but it didn’t work well for other tags

(Mittineague) #2

The ember#### id can’t be safely used (they change)

What I’ve found is that using querySelector works as long as the element can be targeted. eg. (untested)

... querySelector(div[class~="topic-list-item"]);

Or maybe the problem is where / when the JavaScript is being run. (compare view-source against your dev tools elements view)

(SMHassanAlavi) #3

I found the class full name in css and I want to usget elements by class name but I don’ t know how to convert this name.
the name is :
.topic-list a.title.visited:not(.badge-notification)
what is the real complete class name???

(Mittineague) #4

I can’t recall having ever seen this

Where on what page is it?

(Kane York) #5

I think they want to select the topic title on the topic lists.

(SMHassanAlavi) #6

Yes I want to select the title.But I can’t

(SMHassanAlavi) #7

Here it is:

I want to select the topic title but I don’t know how

(SMHassanAlavi) #8

what is query selector.You mean $(’#id’) ??

(SMHassanAlavi) #9

in the front title table

(Rafael dos Santos Silva) #10
document.querySelectorAll(".topic-list .main-link a.title")

is working fine.

(SMHassanAlavi) #11

thanks but this code
var element = document.querySelectorAll(".topic-list .main-link a.title");
returns 0 but I have at least 5 topics

(Rafael dos Santos Silva) #12

Where exactly are you running this code?

Discourse is a Javascript app, so if you execute this code on page start, there is nothing to be seen.

You can read about creating Discourse plugins or about getting a simple callback when things are ready.

(SMHassanAlavi) #13

I use the code here:

I want to create a dynamic table for my site

(Rafael dos Santos Silva) #14

That will not work, as when querySelectorAll runs there is no site yet.

Read the links I posted above.

(SMHassanAlavi) #15

I wrote a plugin like this but it returns 0 again

(SMHassanAlavi) #16

Thank you. I got my answer