how get and element using jquery - javascript

I know how to choose a element using the Attribute Equals Selector [name="value"] but I feeling a little lost when I try to find a check box by his name and value.
this is a example of the checkbox
<input id="check-box-nat-Español" name="kid[native_languages][]" type="checkbox" value="50f97f5304fec25b00000001">
I have a lot of checkbox with the same name, so I need to find the checkbox using the name and the value.
I'm trying this, but it did not work.
$("input[name="kid[native_languages][]"][value=50f97f5304fec25b00000001]").attr("checked",true);​
Any help please!

use single quotes and it should work
$("input[name='kid[native_languages][]'][value=50f97f5304fec25b00000001]").attr("checked",true);
fiddle

You have error while selecting. You should use:
$("input[name='kid[native_languages][]'][value='50f97f5304fec25b00000001']").attr("checked",true)
Link To Fiddle

Use single quotes around your selector so that they wont interfere with the ones inside
$('input[name="kid[native_languages][]"][value=50f97f5304fec25b00000001]')

$("input[name='kid[native_languages][]'][value=50f97f5304fec25b00000001]").prop("checked",true);​

Related

How to get and change the text in a button?

I have a button with the class of
button.btn.btn-default.check-all
how do I find this button, get the text it says and change it with a find?
I tried something like this but not sure if it works
var r = $("button").find("check-all");
It appears that you have a button with multiple classes and you want to select the element based on all the classes. Simply use the following selector
$('button.btn.btn-default.check-all')
You can get the text by
$('button.btn.btn-default.check-all').text()
And can update by
$('button.btn.btn-default.check-all').text('your_updated_text');
You should do something like:
var element = $('button.btn.btn-default.check-all');
//save text in prevText
var prevText = element.text();
//now, change it!
element.text('text-changed');
Using class names to get an element might not be a good solution. My solution would be to use id attribute and use this id attribute to get the correct element.
See this fiddle
HTML
<button id="my-button" name="buttonName" onclick="change()">Hi</button>
and JS
alert(document.getElementById('my-button').innerHTML);
function change(){
document.getElementById('my-button').innerHTML='hello';
}
What I have done here is, used document.getElementById('my-button').innerHTML which gets the text of the button using document.getElementById(). The same thing can be used to set the text in a button.
This question lead to basic skills in jQuery. I truly recommande that you could find out jQuery Api documentation and read deeply into it. You can get a very good view of jQuery thus you can do much better and efficient in your work.
Here is the link that may help you with this question.
https://api.jquery.com/category/selectors/
https://api.jquery.com/category/attributes/
You can use the jQuery :contains() psuedo selector to create a variable that will pinpoint the button with the specified text. It can be any text, but be cautious if you have buttons with similar text because the return is not an exact match (to avoid this only input/search text that you know is unique to the button).
var r = $("button:contains('check-all')");
To change the text with the newly created variable you can use the jQuery .text() method.
r.text('check-none');
Fiddle Example
:contains() jQuery

Change text in dynamically created textbox

I have a bunch of textboxes that are created dynamically one per click.
They all have the same name "discount[]", same id "discount" and have the same class "tinput".
Is there anyway that I can use to change the text in all of these textboxes with javascript or jquery?
I have another bunch of the dynamically created text boxes.
The id and name are unique but the class remains the same.
If it gets the job done, I can change the class name.
Anyhelp is greatly appreciated.
Regards,
You definitely shouldn't use an id more than once per document. If you need it for a <label> you might want to consider putting the input in the <label>.
You are using the class attribute the right way, though. You can do:
$('.tinput').val('New value for all .tinputs!');
If you can't put the input inside the label you can use use this. I made this demo for someone a little while ago. It might be helpful. It'll increment an id like "id-1".
http://jsbin.com/APegOMo/3/edit?html,js,output
same id "discount"
Please, don't do that! "id" is to identify a dom element. That hurts in my soul.
If every id is unique you could access them by $("#myid").val("my new val"). Just an example.
Regards, - Tobbo
here is a simple example that takes all elements with a particular class name and changes the .html, not 100% sure I understand what you're after though. if you need to do someting specific with each divs text you can iterate through them by id (and maybe class as well) and change each divs individual .html.
if you can share a stripped down example of your code or elaborate I'd love to try to help. good luck.
$(".changeDivs").html("adfasdf");
jsfiddle example >>

Raphael setting the attr of an element

I am using raphaeljs add need to set attributes on an element, however the attributes name and value are saved in variables
var attr_name='fill';
var attr_value='#343434';
but the following does not work
exampleelement.attr({attr_name: attr_value});
however if I type in the actual name in the attr field it works
exampleelement.attr({'fill': attr_value});
I have tried wrapping it in quotes and double quotes but this has no effect.
Please can anyone suggest anything?
It is not really an option to manually type them in as this code runs in a loop and each time it could be a different attribute it is setting.
Try this -
exampleelement.attr(attr_name, attr_value);
jQuery Docs - .attr()
Correct syntax is
exampleelement.attr(key, val);
what I found is:
exampleelement.attr({fill:'#343434'});
or
exampleelement.attr('fill': '#343434');
both are same.
Note: There is --> {} <-- in first one.

How do i select an element with certain ID and a certain rel

I looked at selectors and has attribute seems to be the closes choice however i cant seem to make it work.
what i am trying to do i select an element with certain id that also has a certain rel value..
for example:
<input value="Needthisvalue" rel="15" value=wahtever" id="pickthisID" type="hidden">
to find out that input with that id and that rel. what I've tried so far is :
TabID contain the 15.
$('#pickthisID[rel="'+TabID+'"]').val();
Thanks in advanced.
Firstly, your id's have to be unique, otherwise your HTML is erroneous and will throw loads of JS bugs. As for you problem, simply use
$('#pickthisID[rel='+a+']').val();
jQuery is optimized so that, if you provide an ID selector (e.g. #pickthisID), it will stop looking after its found the first matching element. This is because the id attribute is supposed to be unique per page.
However, this would work:
$('[id="pickthisID"][rel="'+TabID+'"]')
As suggested by OP, there is an error in your HTML.
<input value="Needthisvalue" rel="16" id="pickthisID" type="hidden">
console.log($('input[rel=16]').val());
I think your code should work. But you've some problem with html markup.
You use value attribute twice.
<input value="Needthisvalue" rel="15" value=wahtever" id="pickthisID" type="hidden">
But value should one time:
<input value="Needthisvalue" rel="15" id="pickthisID" type="hidden">
jQuery
var TabID= 15;
alert( $('#pickthisID[rel='+ TabID +']').val() )
DEMO
Note
rel is not valid attribute for input. If possible use data-rel.

Changing an ID using JQuery

I'm trying to change the ID of an element here:
http://moemonty.com/chirp/CHIRP-JSON-test.html
By using this line:
$('.databaseID').attr('id', 'test');
I would like to change the id of this line to test, so I can proceed to put in a pre-fix via a string and a variable from JSON data. But for now, I just wanted to see if I could replace it with test at this line:
<li class="databaseID" id="np-44701">
Thanks in advance.
Yes, you can change the ID of an element with $().attr(). The code you give will work, but you should ensure that you only change one element:
$('li.databaseID:first').attr('id','test');
On the other hand, I'd urge you to think this through so you're sure that you really need to change the id of an element. I can't quite imagine the circumstance where it would be necessary.

Categories

Resources