I am trying to get a checkbox name when a checkbox is clicked...i am using a javascript function but it returns undefined.
Here is the code:
notType= document.getElementById($('[type="checkbox"]').attr('id')).value;
alert(notType);
In your code example, you are retrieving the value of the field, rather than the name. Instead, you should use:
var notType = document.getElementById(id).name;
you can do it like this:
$('input[type="checkbox"]').on('click', function(event){
alert(event.target.id);
});
This should work. $("#" + id) finds the element with the specified id. After that, you get the attribute called "name".
var notType = $("#" + id).attr("name");
Related
Hi Everyone I am getting dynamic labors_id now i want to pass this variable labors_id in input[name=labors_id] field but this is not working
HTML
<a id="location[]" class="btn btn-success date-days check-all" href="javascript:void(0);">Select All</a>
Javascript
$(document).ready(function() {
$('.check-all').click(function(){
var labors_id=this.id;
alert(labors_id);
// here I am trying to pass labors_id below but it's not working
$("input[name=labors_id]").attr('checked', true);
});
});
please suggest something how i can pass labors_id variable in input. thank you
To get the checked property(true/false) of the clicked check box
Try
$(document).ready(function() {
$('.check-all').click(function() {
var labors_id = this.id;
$("input[name='"+ labors_id +"']").prop('checked');
});
});
You have to concatenate the variable within the CSS selector in order to get the checkboxes with name labor_id, also you need to use the jquery prop and not attr.
$("input[name='"+ labors_id +"']").prop('checked', true)
Natively speaking, attribute describes the initial/default attribute value of the element while property has the current value/state. Besides, the attribute accepts only string values while property accepts objects, boolean and other types.
use .val() for pass id value to input: http://api.jquery.com/val/
$("input[name=labors_id]").val(labors_id).attr('checked', true);
Works for all j Query version:
$("input[name='"+ labors_id +"']").each ( function ( idx, data )
{
data.checked = true;
});
i tried to search throught the html code for an element named scope using javascript. After that it should show a alertbox with the value of this element.
if($('[name=scope]').length > 0){
var scope = $(this).value();
alert(scope);
}
But that doesn't work for me. What can i do to fix this?
Thanks!
You need val() instead of value(), there is not value function in jQuery
var scope = $(this).val();
Or simply use this.value, that would be simple and faster
var scope = this.value;
Edit based on comments
$('[name=scope]').each(function(){
alert(this.value);
});
I'm trying to take an a element and take the information from the data-id attribute and store it into a variable. I want to then splice this variable so I get the part that I need and implement it into another variable with some text. From that I then want to replace the src attribute of an iframe with the variable. Unfortunately, this doesn't seem to be working at all and I can't find the issue.
Here is the code:
$('.watchvideo').click(function(){
var idu = $(this).attr('data-id');
var id = "//www.youtube.com/embed/"idu.substr(27,37);
$('.videofeatem').setAttribute("src", id);
});
You have 2 issues in code:
1) concatenating substring
2) setting attribute via jquery
var id = "//www.youtube.com/embed/"+idu.substr(27,37);
$('.videofeatem').attr("src", id);
Without seeing the HTML, it's tough to be sure, but a + fixes the obvious problem:
$('.watchvideo').click(function(){
var idu = $(this).data('id');
var id = "//www.youtube.com/embed/" + idu.substr(27,37);
$('.videofeatem').attr("src", id);
});
Also, note that data-xxx attributes can be read by jQuery as .data('xxx')
Simply.
var id = "//www.youtube.com/embed/" + idu.substr(27,37);
Since you're using jQuery, use the .attr() method instead of the .setAttribute().
I want to fetch the id of an element and pass it in jquery function -
$('#fetchedID').fadeOut;
Till now I have tried -
1. $("#$('.delete_status').attr('id')").fadeOut(400);
2. var e = $('.delete_status').attr('id');
$(e).fadeOut(400);
I am sure I am stuck because of the wrong syntax of passing javascript variable in jQuery function. Please help.
Try with concating the Id that you have got with the Id selector(#) like
var e = $('.delete_status').attr('id');
$("#" + e).fadeOut(400);
You have to concatenate the selector, like this:
$("#" + $('.delete_status').prop('id')).fadeOut(400);
If you're going to be using the ID more than once, it is a good idea to cache it:
var delete_status_id = $('.delete_status').prop('id');
$("#" + delete_status_id ).fadeOut(400);
// do something else with delete_status_id...
$("#" + $('.delete_status').attr('id')).fadeOut(400);
Do you really need to pick the ID to then reselect the element and do the fade? If you want to pick only the first occurence of your class, you can use :eq(0) instead.
$('.delete_status:eq(0)').fadeOut(400);
i use such code to access item
function f(id){
$("#"+id).val(); // with analogy $("#id item")
}
is it correct? is any other methods?
If you want to return the value of an element with specified id, then yes as that is what seems to be logical purpose of your function:
function f(id){
return $("#" + id).val();
}
The functions should assume that an element with specified id exists and then it returns you the value of that element. This should work for input fields as well as textarea. If however, it is any other element, you might want to use html() or text() instead of val() eg:
function f(id){
return $("#" + id).html();
// return $("#" + id).text();
}
You could use PureDom
function f(id){
return document.getElementById(id).value;
}
Take that, jQuery!
Yes this is perfectly valid way to access the element having its id.
From the jQuery API website:
.val() Returns: String, Array
Description: Get the current value of
the first element in the set of
matched elements.
What It's not clear to me when you say
// with analogy $("#id item")
is if you want to have ONLY one child item of the one that is identifiedby #id or if you need the item that is identified by item#id.
Your code is perfect if you are passing a string like "hello" inside your code and you want to get the DOM element with ID of #hello.