/    Sign up×
Community /Pin to ProfileBookmark

JS – Table cell function onclick

Hi, its my first post here, I am not great in JS (I am better in html and css)
I want to create one effect:
Let me explain it.
I have random table and when I click on one of cells, I want to get some kind of comment below table and normally that cell become highlighted.
I can draw image of you don’t understand what I mean (image with step 1, step 2 , etc…)
I hope thats its based on JS

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — Try this:

Add a data attribute to your table cells containing the comment:
[code=html]<td dat-comment="your comment for this cell>[/code]Create a container where the comment is displayed in and give it an ID:
[code=html]<span id="tablecomment"></span>[/code]Then place the following script below the table and that container:
[CODE] <script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$("table td").on("click", function() {
var comment = $(this).attr("data-comment");
$("#tablecomment").text(comment);
});
</script>[/CODE]
This is only suitable for comments being relativelay short. For longer comments one would have to use a different solution.
Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — Try this:

Add a data attribute to your table cells containing the comment:
[code=html]<td data-comment="your comment for this cell">[/code]Create a container where the comment is displayed in and give it an ID:
[code=html]<span id="tablecomment"></span>[/code]Then place the following script below the table and that container:
[CODE] <script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$("table td").on("click", function() {
var comment = $(this).attr("data-comment");
$("#tablecomment").text(comment);
});
</script>[/CODE]
This is only suitable for comments being relativelay short. For longer comments one would have to use a different solution.
Copy linkTweet thisAlerts:
@Reikan_HeiwaauthorOct 17.2016 — I tried but nothing displays

I added those codes on right places but nothing happends.

Maybe I have to do something in css ?
Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — I didn't test it yet. Is your page online? Then post the URL. This doesn't need any modifications in the CSS.
Copy linkTweet thisAlerts:
@Reikan_HeiwaauthorOct 17.2016 — Its not online, but in few days it should be online (Had plan to make this work and then to edit some small details and to launch it after that).
Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — In the meantime I tested it and this works fine:
[CODE] <table>
<tr>
<td data-comment="your comment 1">Cell 1</td>
<td data-comment="your comment 2">Cell 2</td>
</tr>
</table>
<span id="tablecomment"></span>
<script>
$("table td").on("click", function() {
var comment = $(this).attr("data-comment");
$("#tablecomment").text(comment);
});
</script>[/CODE]
Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — Sorry, my first posting #2 was faulty. I tried to edit it but created a new posting #3 instead. This is correct:[code=html]<td data-comment="your comment for this cell">[/code]
Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — that cell become highlighted.[/QUOTE]Quite easy to add this:[CODE] $("table td").on("click", function() {
var comment = $(this).attr("data-comment");
$("#tablecomment").text(comment);
$(this).css("background-color", "lightblue");
});
[/CODE]
Copy linkTweet thisAlerts:
@Reikan_HeiwaauthorOct 17.2016 — It works now, only have one question left ?

When I click at one cell it highlights as it should but when I click somewhere around highlight wont dissapear, even If I click on another cell then I have two cells highlighted.

I hope you understand what I mean
Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — Can be fixed easily. This removes the highlighting when a different cell is clicked:[CODE] $("table td").on("click", function() {
var comment = $(this).attr("data-comment");
$("#tablecomment").text(comment);
$("table td").css("background-color", "transparent");
$(this).css("background-color", "lightblue");
});
[/CODE]
Removing the highlighting by clicking elsewhere is possible. Will look into.
Copy linkTweet thisAlerts:
@SempervivumOct 17.2016 — This removes the highlighting and the comment when clicking elsewhere:
[CODE] $("body").on("click", function (event) {
$("table td").css("background-color", "transparent");
$("#tablecomment").text("");
var tg = $(event.target);
if (tg.prop("tagName") == "TD") {
var comment = tg.attr("data-comment");
$("#tablecomment").text(comment);
tg.css("background-color", "lightblue");
}
});
[/CODE]
Better use a class to identify a table cell
Copy linkTweet thisAlerts:
@Reikan_HeiwaauthorOct 18.2016 — Thank you a lot ?

It wokrs perfectly (normally I edited it a bit normally)
×

Success!

Help @Reikan_Heiwa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...