0

I am trying to search my html for class ardiv and then search this class for span. Then i want to get value from this span element using split function, but i am getting this error:

Uncaught TypeError: paranula.split is not a function

    function hledat() {

	var divs = document.getElementsByClassName("ardiv");

	for (var i = 0; i < divs.length; i++) {
		var para = divs[i].getElementsByTagName("span");
		var paranula = para[0];
		console.log(paranula);
		var parasplit = paranula.split(">");
		console.log(parasplit[1]);
	}

    }

    hledat();
<span class="hiddenid">188</span>

5
  • Why are you using the split function and not paranula.id? split is for splitting strings not selecting/filtering elements or fetching attributes. Commented Jun 11, 2017 at 12:32
  • Your code snippet works, so I am assuming that the 0th element returned by getElementByTagName is not the span you are expecting. Try narrowing down on the selector; maybe an id would be a better approach. Commented Jun 11, 2017 at 12:36
  • I see what you're trying to do now, the use of ID was confusing. Instead of parsing the span HTML as a string, just use parseInt( paranula.innerText ) and that will get the id as a number. Commented Jun 11, 2017 at 12:36
  • console.log(document.querySelector('.ardiv .hiddenid').textContent); Commented Jun 11, 2017 at 12:39
  • ´paranula´ contains this ´<span class="hiddenid">188</span>´ so i wanted to split it twice. First using ´.split(">")´ ,second time using ´.split("<")´, that should end up with string containg only value. Commented Jun 11, 2017 at 12:43

1 Answer 1

2

paranula is HTMLElement - kind of JS object, not a string. To access it as a string, use

var parasplit = paranula.outerHTML.split(">");

But if all you need it to take "188" from provided example, use

var result= paranula.innerHTML

The ID of element is other thing - with element like this

<span id="188" class="hiddenid"></span>

you could get "188" with

var result= paranula.id
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the ´var result = paranula.innerHTML´ worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.