Dear All,
This website has taught me lot of things since i was an engineering student. Now again i am back with a new question......


I need to read the contents of the jsp page that is using JSF framework through the Javascript.

Any help for starting the thing will be of great help.

Thanks and regards,
Himadri Sikhar Khargharia
or mdew_47

Dani AI

Generated

@md ew_47 — most likely causes: the script runs before the DOM element exists, the JSF runtime changed the element's client-side id, or code is dereferencing null (which throws an error and stops execution). Your snippet shows a span with a namespaced id (colon). A span has no value property, so use textContent/innerText/innerHTML as appropriate.

Try this safe, minimal pattern — it waits for the DOM, attempts a direct lookup, and falls back to a JSF-friendly search:

document.addEventListener('DOMContentLoaded', function () {
  var el = document.getElementById('help:firstname'); // works if id is exactly this
  if (!el) {
    // JSF may prefix ids; match by suffix
    el = document.querySelector('[id$=":firstname"]');
  }
  if (el) {
    console.log('text:', el.textContent.trim());
  } else {
    console.warn('element not found — open DevTools and inspect the rendered id');
  }
});

Troubleshooting checklist:

  • Open browser DevTools and confirm the rendered id of the element. JSF often adds naming-container prefixes.
  • If you use querySelector with an id that contains :, escape it ('#help\\:firstname') or prefer getElementById.
  • Check the Console for errors like “Cannot read property 'innerHTML' of null”; such errors stop subsequent code (explains why your third alert never ran).
  • If you rely on jQuery: $('#help\\:firstname').text() (backslash escapes the colon).
  • If you want the form input value (not a span), ensure the element is an <input> and use .value.

These steps should let you reliably find the element and read its contents in a JSF page.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

    </head>
    <body id="himadri">
        

            <h1>Hello World!</h1>
            
<form id="help" name="help" method="post" action="/Himadri-war/faces/WEB-INF/See.jsp" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="help" value="help" />
<table>
<tbody>
<tr>
<td class="rightalign"><label>
Userid: </label></td>
<td class="leftalign"><span id="help:userid">88</span></td>

</tr>
<tr>
<td class="rightalign"><label>
FirstName: </label></td>
<td class="leftalign"><span id="help:firstname">tght</span></td>
</tr>
<tr>
<td class="rightalign"><label>
Lastname: </label></td>
<td class="leftalign">ghg</td>
</tr>
<tr>
<td class="rightalign"><label>

Gender: </label></td>
<td class="leftalign">gr</td>
</tr>
<tr>
<td class="rightalign"><label>
Age: </label></td>
<td class="leftalign">43</td>
</tr>
<tr>
<td class="rightalign"><label>
Details: </label></td>
<td class="leftalign"><span id="help:Details"></span></td>

</tr>
</tbody>
</table>
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="2606324558782240815:-4303962549239945749" autocomplete="off" />
</form>
        </body>
</html>

I need to read a page like this... I am using:

function Apply(){
alert('hello first1');
var frm = document.getElementById("help:firstname");
alert(frm);
alert('hello second2');
}

But in this case alert(frm) is showing null.... Please help me to find out the proble, by using innerHTML...even the third alert statement is not executed.... I need to get the value of the element by Id "help:firstname"...

Please help..thanks and regards
himadri

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.