I have an input button where is written within a blade foreach loop in a laravel project.
<div class="v-percentage">
<input id="copycode" type="Submit" value="Embed">
</div>
I am trying to write a script that will allow me to add a target attribute on the input so each button would be unique, and do something like this answer here. The script I wrote so far it should get the element and then within a for loop to add the target which i am not sure yet how I should do this.
var elements = document.getElementById('#copycode');
alert(elements);
for(var i = 0; i < elements.length; i++)
{
//add the target to the element
}
Also, when I alert the elements is returning null. How can i work that out?
document.getElementById('copycode')
getElementById just wants the ID, not a CSS selector.
Also, this will return one element (not a NodeList). IDs are unique throughout the page. If you want to select multiple elements, use classes and getElementsByClassName.
the Javascript function getElementById only returns one element. If you are trying to get a set of elements, try to use a class
<input class="copycode" type="submit" value="Embed" />
Then you could use a jquery selector to get the set of elements and iterate over then
$(".copycode").each(function(key, element){
element.attr("target", "foo");
});
If you don't want to use jquery, use the javascript function getElementsByClassName and a for to iterate over the set of elements
var elements = document.getElementsByClassName("copycode");
for (var i = 0; i < elements.length; i++) {
elements[i].setAttribute("target", "foo");
}
You can't have more than one ID with the same name, ID should be unique per page.
The getElementById() method accesses the first element with the specified id.
If you want get all the input element then you should try getElementsByTagName()
The getElementsByTagName() method accesses all elements with the specified tagname.
document.getElementsByTagName("input");
var elements = document.getElementsByTagName("input");
alert(elements);
for(var i = 0; i < elements.length; i++)
{
//add the target to the element
}
Hope this helps!
Related
So I have this HTML code:
<p style="margin-bottom: 5px"><span id="" class="stars-container stars-custom">★★★★★</span></p>
basically I want to assign a unique id to each instance of this element, so first element id="1", second element id="2" etc.
I was thinking of using getElementsByClassName to identify them and grab the class name, but how do I assign and a unique id?? I'm pretty new to JS so I don't get how the loop will look like.
This will handle. You basically select all of the p elements with the classes you indicate, loop over them and set the id for each of them to the index of the loop variable.
I could not use a foreach loop as it's not usable on HTMLCollections
const paragraphs = document.getElementsByClassName('stars-container stars-custom');
for (let i = 0; i < paragraphs.length; i++) paragraphs[i].setAttribute('id', i + 1);
I want to add a class to a specific element of an element array, but I want to add it based on a variable of the for loop, not just a random number, and add it to more than one elements if I have to, but the point is, I need to search the elements with the variable given to me!
for(var a = 0; a < array.length; a++){
$('.results').append('some html divs here');
if(array[a].id == $.cookie('userId')){
$('.buyButton').addClass('disabled'); // here's my problem I don't want to add the
//disabled class to every element with the buyButton class, but just the one selected by the if statement
}
}
Thank you!
It looks like your array is an array of divs, if so you can use your array rather then the class as your jQuery object to alter the class, as so
for(var a = 0; a < array.length; a++){
$('.results').append('some html divs here');
if(array[a].id == $.cookie('userId')){
array[a].addClass('disabled');
}
}
Is it possible to add an HTML ID via the browser console using DOM Manipulation?
For example, suppose I am dealing with the following element:
<div class="elementClass">
I can fetch via DOM with something like:
document.getElementsByClassName("elementClass");
However, is it possible to add an attribute via this type of method? If I inspect the element with chrome I can do it manually, but I am wondering if it's possible to inject that ID via JavaScript.
Thanks!
You can do:
document.getElementsByClassName("elementClass")[0].setAttribute("id", "some ID");
Yes you can modify attributes of an object (HTMLElement) via javascript
getElementsByclassName returns an array, simply iterate through the list and set the attributes you wish.
var elements = document.getElementsByClassName("elementClass");
for(var I = 0; I < elements.length; I++)
element[I].id = "element" + I;
Then you could access any of those elements with that class via
var element = document.getElementById("element" + 0); //Gets First Element
Sure.
document.getElementsByClassName('elementClass')[0].id = 'someID';
or
document.getElementsByClassName('elementClass')[0].setAttribute('id', 'someID');
Just know that if you grab elements using getElementsByClassName then it will return an array of elements (assuming any elements with the matching class exist) so modifying the array won't give you the result you want.
setAttribute() method on the DOM element should work just fine.
HTML
<div class="elementClass">
This is the content of elementClass
</div>
<button onclick="buttonClicked()">Click to add attribute id red to elementClass</button>
Javascript
function buttonClicked(){
document.getElementsByClassName("elementClass")[0].setAttribute("id", "red");
}
CSS
#red {
background-color: red;
}
PLAYGROUND
Note that manipulating the class attribute causes the browser to reflow, therefore it is worth mentioning to use setAttribute() wisely to not cause performance issue.
<body>
<span someAttribute="xyz">.....</span>
...
<div>
...
<span someAttribute="abc">.....</span>
...
<div someAttribute="pqr">.....</div>
...
</div>
</body>
Here is a sample html page.. I need to select the html elements by its attributes i can get the attribute values by getAttribute() but i need to select all the elements first.
How in javascript to get elements which has the attribute name as "someAttribute". Once i get the elements i can get the attribute values and use my function.
Note: i want to do this without using jquery.
In new browsers you can do:
var el = document.querySelector('[someAttribute="someValue"]');
store each element in a array the loop throught each element, and if the element contains the attribute someAttribute do somgthing.
var arr_elms = [];
arr_elms = document.body.getElementsByTagName("*");
var elms_len = arr_elms.length;
for (var i = 0; i < elms_len; i++) {
if(arr_elms[i].getAttribute("someAttribute") != null){
alert("FOUND : " + arr_elms[i].getAttribute("someAttribute"));
}
}
You can select elements by tag name using document.body.getElementsByTagName("div") to get all the div elements inside your document. This function returns an array of elements, which you can parse and filter out elements that don't match your criteria.
You can traver all elements of DOM tree.
you can use
var all = document.getElementsByTagName('*');
but this also returns the html, head and body ...
and then do a loop over all elements and look for the attributes.
I found a snippet called getElementsByAttribute(doc, tagArray, attribute, attributeValue)
You can give a try to a working fiddle: http://jsfiddle.net/Yx7EU/
Hope this can help.
I need a solution to remove a div without an ID tag in JavaScript only. The div looks like this <div align="center">.
Here is the full structure.
<tr id="-1">
<td class="stxt">
<div align="center">
</div>
</td>
</tr>
You need to work out how to get a reference to it, once you've done that, you can remove it using:
div.parentNode.removeChild(div);
Of course the align attribute has been deprecated, but anyway, you can find the divs with align="center" using:
var divs = document.getElementsByTagName('div');
var div;
var i = divs.length;
while (i--) {
div = divs[i];
if (div.getAttribute('align') == 'center') {
div.parentNode.removeChild(div);
}
}
Which will remove every div in the document that has align="center".
Note that the object returned by getElementsByTagName is a NodeList. If you iterate over it from 0 and remove nodes, they are removed from the live list so you will skip the node following the one you remove and you will attempt to access no existant nodes at the end. Going over the list backwards avoids these pitfalls. An alternative is to turn the NodeList into an array, but that's somewhat inefficient.
Edit
Since you edited the question, here's an update answer.
You can get a reference to the TR using getElementById:
var root = document.getElementById('-1');
Now you can go down the DOM:
var cell = root.cells[0]; // First cell in the row
var div = cell.getElementsByTagName('div')[0]; // first div
cell.removeChild(div);
Which is specific to the structure you've posted.
If you can just remove the content, you can use:
var all = document.getElementsByTagName("div");
for(i=0; i < all.length; i++) {
all[i].innerHTML = ""
}
See http://jsfiddle.net/7KVkC/
You need some way to make this div tag unique. There is another javascipt function called getElementsByTagName that you can use to get an array of all of the div tags. You can then use the DOM to check whether that div tag has the property of align="center".