Jump to content

referencing a sibling with the class of....


djp1988

Recommended Posts

I am trying to set a variable which is the innerHTML of a span, i am doing this when i slick a span within the same paragraph as the other, so they are both siblingsso onclick for the span i want, i am doing this function, but I don't know how to reference the sibling, I passed the id of the span what was clicked, got the parent node and then I try to get all children with the tag <span> and then select the ones where the class name is equa to the one I am targetting, but obviously this isnt how it's done because it doesn't work.Any suggestions?

	function editTXTField(id){getIT = id.parentNode.childNodes("span");	for(vall=0; vall<getIT.length;vall++){		if(getIT[vall].className =="editableTXT"){			infos = getIT[vall].innerHTML;			}		}}

My html:

<p class="lineEdit"><strong>Site</strong>: <span class="editableTXT"><em>Not given</em> </span><span class="EDITLINK">EDIT</span></p>

So in the end, I aim to click on the word EDIT which sets a variable called infos, the value would be: infos = "Not given";

Link to comment
Share on other sites

Rather than element.parentNode.childNodes("span"), you might try something more along these lines:

var myspecialspan;var spans = element.parentNode.getElementsByTagName("span");for(var i = 0; i < spans.length; i++){	if(spans[i].className == "editableTXT")	{		myspecialspan = spans[i];	}}if(myspecialspan){	info = myspecialspan.innerHTML;}else{	info = "";}

Link to comment
Share on other sites

Ok thank you, amungst other things i also want to alert() the innerHTML of a textfield, but I created the textfield on the fly in another function, so it isn't in the DOm, or is it? How can I alert this, I mean reference this if it isn't in the mark up?ALSOHow do I go about checking the length of a node? as in how many letters are within a childNode

Link to comment
Share on other sites

When you create new nodes, you change the DOM, so it IS in the current markup. It's simply not in the source, which is irrelevant. All you need is the usual reference: by node, ID, or collection-item.For the length, node.length will get you text + HTML. textContent.length is what you really want, but it's supported only by newer browsers. innerText.length might be available also. So try a statement like this:len = node.textContent.length || node.innerText.length;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...