Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get element's value without knowing its name?

Status
Not open for further replies.

markros

Programmer
May 21, 2007
3,150
US
Hi everybody,

My colleague has this function
Code:
function openCreateID(pid){
	var pid=document.getElementById(pid).firstChild.id;
	var _ids=new Array();
  	_ids=pid.split("_");
	var _id="";
  	for (i=0;i<_ids.length-1;i++){
		_id+=_ids[i]+"_";
	}
	_id=_id.substring(0,_id.length-1);
	var qNames=new Array("Address1","Address2","City","State","Zip","Email","ScreenName","HomePhone","CellPhone");
	var q="?ptype=N&perid=0&pid="+pid;
	for (i=0;i<qNames.length;i++) {
		q+="&"+qNames[i]+"=" + document.getElementById(_id+"_"+qNames[i]).value;
	}
etc.

However, I switched later to user controls, so the my control names are now different. (They would include the UserControl name, I guess).

So, is there a way to search for an object inside particular div tag but without knowing its full name, only know part of the name?

Thanks in advance.
 
Maybe something like this:
Code:
for(var c=myDiv.childNodes, len=c.length, i=0; i<len && !c[i].id || c[i].id.indexOf( myPrefix )!= 0; i++)
;

var elem = ( i == len ? null : c[i] );

 
[0] Before modifying the existing function, some comments are in order.
[0.1] This line
>var pid=document.getElementById(pid).firstChild.id;
seems to be very much at the mercy of the concrete constraint imposed on the scripters that they do not put any insignificant writespaces after the element with id of pid. If not, there will be browser-dependent issue.
[0.2] The pid being changed in the significance inside the function should be fine in practice but better restraint from doing so. The end result is that pid becomes the "firstChild" (supposed to be some form element(?)) id taken out the last part of splitting underscore. This I suppose is the prefix in constructing other input element's id. In the modified scenario, that prefix would it still be significant? (Also, [0.1] note is acutely significant.)

[1] Suppose the prefix mentioned in [0.2] is no longer significant. And suppose the characteristic particle for each component of the query string is from the qNames array. The approach would be something like this.
[tt]
function openCreateID(pid) {
var qNames=new Array("Address1", "Address2", "City", "State", "Zip", "Email", "ScreenName", "HomePhone", "CellPhone");
//var q="?ptype=N&perid=0&pid="+pid;
var q="?ptype=N&perid=0"; //pid part taken out, need clarification
var obj=document.getElementById(pid); //probably a form?
for (var i=0;i<qNames.length;i++) {
for (var j=0; j<obj.elements.length;j++) {
if (obj.elements[j].id.indexOf(qNames) != -1) {
q+="&"+qNames+"=" + obj.elements[j].value;
}
}
}
alert(q); //inspection: to comment out later
return q //???
}
[/tt]
[1.1] The exercise is valid only if qNames is totally distinct entries, no overlapping substring (such as array("addr","address","address1")).
[1.2] The query may have to be properly (uri) encoded: that is not scripted into it.
[1.3] The function would build the query string for a typical form structure like this, with pid passing in "abc".
[tt]
<form id="abc">
<input id="xxxAddress2yyyy" name="somename" type="text" />
<input id="vvvvvZip name="someothername" type="text" />
<!-- etc etc -->
</form>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top