Change text in dynamically created textbox - javascript

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 >>

Related

Using placeholder as the typical id attribute in JavaScript

This question will probably either be extremely someone or flat out impossible. Inside of JavaScript how would I go about changing the value attribute of a input tag in html based on the placeholder value?
This question does sound dumb as a normal person would just set each of the divisions with different ids and change it that way. However, I’m creating an extension for a page with JavaScript that doesn’t have id values on the input tags.
Note: all of the placeholder texts are different, there are pretty much no id parent divisions except the wrapper, all the classes are the same for the input tag I wish to modify.
Thanks in advance. (It’s a dumb question I know. Forgive me)
Html I’m editing
<input type=“tel” maxlength=“1” placeholder=“1” value class=“UIInput-input>
It’s the same thing for the rest but each time the place holder goes up by one.
To find elements in the DOM based on attributes other than ID or Class, you can use the document.querySelector() method.
In your particular example, you would use it like this:
var newValue = 'my new value for my input element';
document.querySelector('input[placeholder="1"]').value = newValue;

Is there a way of selecting elements by their title in JS?

I am trying to create a program to select elements on the page, but it seems that the only thing that makes them unique are there titles. Is there any way of selecting certain things on a web page by their titles? I found this script but it will not select or click on the elements.
What am I doing wrong? Any help will do. Thanks
How can I get it to work on A and not div, sorry, that was the problem
My script that I am using --
$(document).ready(function () {
$("a[title=\"Learn More About Becoming A VIP\"]").click();
});
It says Div is not defined when I try to run script. Why So?
There is a way to select elements by their title attribute in javascript:
document.querySelectorAll('div[title="john"]');
======
You can also select elements that have a title beginning with john:
document.querySelectorAll('div[title^="john"]');
Or elements that have the (space-bounded) word john somewhere in their title:
document.querySelectorAll('div[title~="john"]');
Your selector should work if title value is john. You might wan't to select element that contains certain string as follow:
$('div[title*="john"]')

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

select elements that don't contain a specific image with jquery

Hi I am trying to use jQuery to select all elements which have the class of "icon" which do not contain a particular image (to turn them off for filtering content)
I could do something like the example below, but it would seem like there would be a much cleaner way to select all elements that don't contain that image src to hide them.
Thank you for your feedback!
$('.icon').hide();
$('.icon').find('img[src$="'+src+'"]').parent().parent().show();
$('.icon').find('img[src !="'+src+'"]').hide(); may work

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