html - Javascript - How to get attribute value from a tag, inside a specific div class? -


snippet of html code need retrieve values from:

<div class="elgg-foot">   <input type="hidden" value="41" name="guid">   <input class="elgg-button elgg-button-submit" type="submit" value="save"> </div> 

i need value 41, simple enough with:

var x = document.getelementsbytagname("input")[0]; var y = x.attributes[1].value; 

however need make sure i'm retrieving values inside "elgg-foot", because there multiple div classes in html code.

i can class this:

var = document.getelementsbyclassname("elgg-foot")[0]; 

and tried combine in various ways var x, don't know syntax/logic it. example:

var full = a.getelementsbytagname("input")[0]; 

so: retrieve value 41 inside unique class elg-foot.

i spent hours googling this, couldn't find solution (partly because don't know search for)

edit: answers everyone, seem work. had working myself, forgot [0] somewhere in original code. appreciate jquery well, never used before :-)

the easiest way use jquery , use css selectors:

$(".elgg-foot") indeed element class "elgg-foot", if go 1 step further, can use descendent selectors:

$(".elgg-foot input[name='guid']").val() 

that ensures input named guid child of element labelled class elgg-foot.

the equivalent in modern browsers native queryselectorall method:

document.queryselectorall(".elgg-foot input[name='guid']") 

or can have yourself:

var x = document.getelementsbyclassname("elgg-foot") var y = x.getelementsbytagname("input")[0]; 

assuming know first input within div


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -