Very simple javascript request. Grab element ID - javascript

I have an element that I'm grabbing the value, thus:
var text = form1.elements[i].value;
What I want to know is if I can grab form1.elements[i].value's ID somehow.

var id = form1.elements[i].id;

Related

Get text value of html elements with same #Id

<a id="s_6_2_19_0_mb" class="siebui-ctrl-drilldown" name="Name" rowid="1" href="javascript:void(0)">00102400010630001</a>
<a id="s_6_2_19_0_mb" class="siebui-ctrl-drilldown" name="Name" rowid="2" href="javascript:void(0)">00102402608820001</a>
I need to get the text values of both these elements. so I use the following code:
$('a#s_6_2_19_0_mb[rowid="1"]').text();
$('a#s_6_2_19_0_mb[rowid="2"]').text();
But the #Id will differ from page to page. So I am getting the id in a variable, say "idVal". And idVal = s_6_2_19_0_mb. So now to get the text value I used the below code
$('\'a' +'#'+idVal+'\[rowid\=\"1\"]'+'\'').text();
But this throws me an error. Please let me know how to extract the text value using a variable for #id in the above case
Well, as others have said IDs must be unique, but if you really can't change that (and assuming classes and rows may differ), then you could do:
$("a[id='"+idVal+"'][rowid='1']").text();
You may simplify your selector and remove the a and use the following code where rowId holds the id of the row you wish to retrieve:
var linkText = $('#'+ idVal +'[rowid=\"'+ rowId +'\"]').text();
or just hardcode the row id like that:
var link1Text = $('#'+ idVal +'[rowid="1"]').text();
var link1Text = $('#'+ idVal +'[rowid="2"]').text();
Cheers,
Id be unique in HTML so use class instead of id
$(".siebui-ctrl-drilldown[rowid = 1]").text();
$(".siebui-ctrl-drilldown[rowid = 2]").text();
You don't need to escape the =, nor the "s, and you have an uneven number of quotes.
So you should have something like this:
$('a' +'#'+idVal+'[rowid="1"]').text();
Oh and yeah, as everybody else said, IDs should always be unique. Trying to access multiple elements with the same ID will always return the first one.
You should never have same IDs for multiple elements on same page. however You can use class selector to target them:
$("a.siebui-ctrl-drilldown[rowid=1]").text();
$("a.siebui-ctrl-drilldown[rowid=2]").text();
like that:
$('a#' + idVal + '[rowid="1"]').text();
However you have to avoid same id in DOM
You can simply use this:(Remove ids with same value)
$("a[rowid=1]").text();
$("a[rowid=2]").text();

How to get all elements in "parentNode" whose id starts with same string?

I have a function that gets element's id and finds out its parentNode, now I want to find all the elements whose id starts with same string, in that parentNode.
Is there any function or way to find out that?
I am trying this:
var allElements = document.querySelectorAll('*[id^="someString"]');
but this returns me a list of elemets from entire document, but I just want to get the elements from the same parentNode.
Please help!
Elements have QSA too:
var elements = myElement.parentNode.querySelectorAll('*[id^="someString"]');

Grabbing the ID attribute of a selected element within my svg-canvas. SVGEDITOR and Methoddraw

I would like to get the id of a selected element within my svg-canvas as a variable to use it within a function.
I would take it that var selected = svgCanvas.getSelectedElems; doesn't pass a particular id.
Any ideas?
You can make direct use of selectedElement.id
var elementSel = selectedElement.id;

Changing the ID of html elements

I have a form which allows user to add elements(which might be a field set or a text box) dynamically. I'm able to assign a new ID to the elements when added but I'm not able to make it in a sequence as the user can add elements in between as well.
So for example, there is an Id named XXX1 and the user adds a new element after it which is xxx2. Now if the user adds a new element again after XXX1, it comes up as XXX3. So the order of the elements is XXX1, XXX3, XXX2. I'm not able to control the names when it is being added. So I'm trying to re-assign the names after add.
I'm trying to get all elements in an array and change the ID as follows
document.getElementById('xxx3').setAttribute('id', 'xxx2');
But this doesn't work as ID XXX2 already exists for another element. Please help me with a solution for this.
So why not change the ID of xxx2 first, to move it out of the way, then putting it back in place later?
document.getElementById('xxx2').setAttribute('id', 'temporaryId');
document.getElementById('xxx3').setAttribute('id', 'xxx2');
document.getElementById('temporaryId').setAttribute('id', 'xxx3');
Why not go from the last element to the first(the one's after the position you are inserting to) and add one to the ID?
for example:
var insertAt = 2; // for an element to be called xxx2
var els = [...]; // an array with all of the elements
if(els.length >= insertAt){
for(var i = els.length-1; i >= insertAt-1; --i){
els[i].setAttribute('id', 'xxx'+(i+2));
}
}
// Add the new element here which will be called xxx2

How to dynamic select element with JQuery by id

I need to dynamic select element with JQuery, I get in code id of element. How to do that ?
I've tried:
var sel='\'#'+id+'\'';
var elem+$(sel);
but it doesn't work ( id is string id of element).
You would use code such as
var ID = 'whatEver';
$('#' + ID).action();
You would then be able to use that to select whatever element you are after.
You don't need the extra quotes. Just:
var elem = $("#"+id);
A live example:: to find text area with attribute data-comment-id with any value
var value = "anyValue";
$('textarea[data-comment-id='+value+']')

Categories

Resources