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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Current Id from Tag Element

Status
Not open for further replies.

JBorges

Technical User
Jan 12, 2009
57
DK
Hi.

I'm looking for a way to get the currently selected id from a tag element. I'm building an iPhone Bible app and as a way for the user to see where he is I extract the title and verse reference into a label. I can extract the title just fine but I need to get the numbers like '1:4' from the 'bs' tag as well. I'm thinking I need a function that loops through all the 'bs' tags and displays the one just selected, rather than having to write document.getElementsByTagName('bs')[0, 1, 2, 3 etc].innerText; a hundred times, but just what does it look like?

This is what my tags look like. I've also included a function I wrote. It gets all the ids and displays them one at a time in an alert panel. Naturally that is not what I want, but it's a start. Hopefully someone can help me out here:

Code:
function getCurrentId(what) {
    
    var getId= document.getElementsByTagName('bs');
    
    for (i=0; i<getId.length; i++) {
        alert(getId[i]);
    }
    
    
}


<bs id="1:4" onClick="getCurrentId(this);">
    <b> 4</b></bs>
    <vs id="vs4">“Is it the time for YOU yourselves to dwell in YOUR paneled houses, 
        while this house is waste?
    </vs>

<bs id="1:5"  onClick="getCurrentId(this);">
    <b> 5</b></bs>
    <vs id="vs5">And now this is what Jehovah of armies has said, ‘Set YOUR heart upon YOUR ways.
    </vs>

<bs id="1:6"  onClick="getCurrentId(this);">
    <b> 6</b></bs>
    <vs id="vs6">YOU have sown much seed, but there is a bringing of little in.
        There is an eating, but it is not to satisfaction.
        There is a drinking, but not to the point of getting intoxicated. There is a putting on of clothes, but it is not with anyone’s getting warm; and he that is hiring himself out is hiring himself out for a bag having holes.’”
    </vs>
 
The following works for me :

Code:
<html>
<head>
<script language="javascript">
function getCurrentId(what) {

    alert(what.id);

}
</script>
</head>
<body>
<bs id="1:4" onClick="getCurrentId(this);">
    <b> 4</b></bs>
    <vs id="vs4">"Is it the time for YOU yourselves to dwell in YOUR paneled houses,
        while this house is waste?
    </vs>
</bs>
<bs id="1:5"  onClick="getCurrentId(this);">
    <b> 5</b></bs>
    <vs id="vs5">And now this is what Jehovah of armies has said, 'Set YOUR heart upon YOUR ways.
    </vs>
</bs>
<bs id="1:6"  onClick="getCurrentId(this);">
    <b> 6</b></bs>
    <vs id="vs6">YOU have sown much seed, but there is a bringing of little in.
        There is an eating, but it is not to satisfaction.
        There is a drinking, but not to the point of getting intoxicated. There is a putting on of clothes, but it is not with anyone's getting warm; and he that is hiring himself out is hiring himself out for a bag having holes.'"
    </vs>
</bs>
</body>
</html>

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Why not simply have onClick="getCurrentId(this.id) then parse the child elements.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thank you. Both suggestions work and get the id value of the tag that is clicked.

Yet, the more I dig into this I believe this is actually more an Objective-c issue. I'm calling a JS-function through an objective-c method. When the webview has finished loading the HTML pages and its anchor tag then it should get the title and the id of currently selected anchor tag. It should fetch it, not onclick, but when the HTML document has finished loading.

I thought I could do it with something like this:
Code:
document.getElementsByTagName('bs')this.id;
;

That's why I mentioned the loop; when the webview has finished loading then it should loop through the 'bs' tags and get the id of the currently selected anchor tag.

Maybe this is the wrong forum.

/Philip
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top