I want get the id of the element that is clicked and within a specified class, and want to print the ID.
Here is my JS , I am really new JS , Please help
$('.wrapinner').click(function(e) {
$('.wrapinner').css("margin-top","0");
$('.wrapinner').css("opacity","0.4");
$(this).css("margin-top","25px");
$(this).css("opacity","1");
var r= document.getElementById(this).attributes.toString();
document.write(r);
});
There are two ways to do this:
1) Done without jQuery object (it is faster)
$(".wrapinner").click(function(){
//Your Code
var id = this.id;
document.write(id);
});
OR
2) Done with jQuery object:
$(".wrapinner").click(function(){
//Your Code
var id = $(this).attr('id');
document.write(id);
});
NOTE after jfriend00's comment.
Use document.write() if really needed and is not harmful to your page OR use console.log(). Read Why is document.write considered a "bad practice"?
You can call the native javascript object directly; this.id.
$('.wrapinner').click(function(e) {
$('.wrapinner').css("margin-top","0").css("opacity","0.4"); // chained these two
$(this).css("margin-top","25px").css("opacity","1"); // chained these two
var r = this.id; // this should already reference the correct element.
document.write(r);
});
Try this way :
$('.wrapinner').click(function(e) {
console.log(this.id);
});
You are not passing id to getElementById, rather you are passing the object it self. You can get id of source object of event using this.id not by this.
Change
var r= document.getElementById(this).attributes.toString();
To
var r= document.getElementById(this.id).attributes.toString();
Just write
document.write(this.id)
$(".wrapinner").click(function(){
var id = this.id
});
Try this example:
<table>
<tbody>
<tr id="CustomerScreen" class="rows"></tr>
<tr id="TraderScreen" class="rows"></tr>
<tr id="DistributorScreen" class="rows"></tr>
</tbody>
</table>
<script>
$('.rows').each(function () {
var ar = this.id;
console.log(ar);
});
</script>
You can check this code..
$('.wrapinner').click(function(e) {
var getID = $(this).attr('id');
alert(getID);
});
Try this:
$('.wrapinner').click(function(e) {
$('.wrapinner').css("margin-top","0");
$('.wrapinner').css("opacity","0.4");
$(this).css("margin-top","25px");
$(this).css("opacity","1");
var id = $(this).attr('id');
});
I am capturing click event on class and then finding its id and class name using parent().
demodemo
$(document).ready(function(){
$('.class-first ul li, .class-second ul li, .class-third ul li').on('click',function(){
console.log("child :"+$(this).attr('id') + " Parent:"+$(this).parents().eq(1).attr('class'));
});
})
Related
I'm grabbing the value of an input and setting this as a variable. How can I then use this as part of the selector name. Here's a demo of where I've got too and what I'm trying to achieve
// grab the id
var rowId = $('input[name="row-id"]').val();
// Use the variable as part of selector
$('body').on("click", ".upload-form-'rowId'", function(event) {
// do stuff here
})
The output of the selector would be like .upload-form-99 for example
Why not just do this:
// grab the id
var rowId = $('input[name="row-id"]').val();
rowId = '.upload-form-' + rowId;
// Use the variable as part of selector
$('body').on("click", rowId, function(event) {
// do stuff here
})
Any particular reason that this wouldn't work?
$('body').on("click", ".upload-form-" + rowId, function...
Here's a more elegant solution if you happen to be using ES6:
// grab the id
let rowId = $('input[name="row-id"]').val();
// Use the variable as part of selector
$('body').on("click", `.upload-form-${rowId}`, (event) => {
// do stuff here
})
if I use:
jQuery(document).ready(function() {
console.log($(this));
});
then in console I have object:
[Document mypage.html#weather]
how can i get for this last ID? In this example this is #weather. I would like use alert #weather in selector, for example $(data_from_console + '.add').val();
Do you want to get the last element that has an ID attribute?
If so:
$(document).ready(function () {
console.log($('body *[id]').eq(-1));
});
EDIT
On a closer look, are you looking for the hash tag? ie. if your URL is mypage.html#weather you want the #weather ?
In that case try:
$(document).ready(function () {
console.log(document.location.hash);
});
You can use last() method. Try this:
$(document).ready(function() {
$('body *').each(function() { //selects all elements in body which got id
var lastEle = $(this).last(); //selects last one of them
console.log(lastEle.attr('id'));//returns it's id to console log
});
});
How can I get the onclick event (this value change when click on div) in jQuery?
$("ul li").click(function(){
var v = $(this).val();
});
$(function(){
alert(v);
});
$(function() {
var v;
$('ul li').click(function() {
v = $(this).val(); // .text() - consider
testFunction();
});
function testFunction() {
alert(v);
}
});
Your issue was variable scope, you were defining a variable only available to the click function. If you define it outside of the function it will be available to other in scope functions.
Here's a demo:
http://jsfiddle.net/vs87B/
You might want to consider using .text() instead of .val()
If what you mean getting the innerHTML of the div when clicked, here is a code that could help
$("div").click(function(){console.log($(this).html())})
as I understand that you want to change the text of a div
your div is as follow
<div>123</div>
you can do it as
<script>
$("div").click(function () {
$(this).replaceWith( "new text " );
});
</script>
EDIT
you can use global variable as follow
$.mynamespace = {};
$("ul li").click(function(){
$.mynamespace.myVar = $(this).val();
});
$(function(){
alert( $.mynamespace.myVar );
});
refer How to store a global value (not necessarily a global variable) in jQuery?
If I have id's of the form: t_* where * could be any text, how do I capture the click event of these id's and be able to store the value of * into a variable for my use?
Use the starts with selector ^= like this:
$('element[id^="t_"]').click(function(){
alert($(this).attr('id'));
});
Where element could be any element eg a div, input or whatever you specify or you may even leave that out:
$('[id^="t_"]').click(function(){
alert($(this).attr('id'));
});
Update:
$('[id^="t_"]').click(function(){
var arr = $(this).attr('id').split('_');
alert(arr[1]);
});
More Info:
http://api.jquery.com/attribute-starts-with-selector/
var CurrentID = $(this).attr('id');
var CurrentName = CurrentID.replace("t_", "");
That should help with the repalce.
Try this:
// handle the click event of all elements whose id attribute begins with "t_"
$("[id^='t_']").click(function()
{
var id = $(this).attr("id");
// handle the click event here
});
$("[id^='t_']").click(function()
{
var idEnding = $(this).attr("id");
idEnding.replace(/\t_/,'');
});
Using the click event capture the ID that begins with t_, then replace that with nothing giving a capture the end of the ID value.
Couldn't find an answer, but Im trying to get the last ID of the last div that was created on the page. The code I'm using doesn't seem to work using .last(). My syntax is probably wrong but I can't get it to show the ID.
jQuery(document).ready(function()
{jQuery("a.more_posts").live('click', function(event){
jQuery("div[id^='statuscontainer_']").last();
var id = parseInt(this.id.replace("statuscontainer_", ""));
alert(id);
});
});
I figured out to do it this way and this worked.
jQuery(document).ready(function(){
jQuery("a.more_posts").live('click', function(event){
jQuery("div[id^='statuscontainer_']:last").each(function(){
var id = parseInt(this.id.replace("statuscontainer_", ""));
alert(id);
});
});
});
The this.id is going to give you the id of the link that was clicked. I think you want the id of the last container.
jQuery(document).ready(function() {
jQuery("a.more_posts").live('click', function(event){
var lastContainer = jQuery("div[id^='statuscontainer_']").last();
var id = parseInt(lastContainer.attr('id').replace("statuscontainer_", ""));
alert(id);
});
});