Brandon
Brandon

Reputation: 2171

Pass Text to JavaScript with document.getElementById

I am trying to pass text to JavaScript via document.getElementById. Specifically, I am trying to pass the value of the ID (set to "Jason_Example_12" in the example below). I can pass values that are integers, but am unable to pass text.

I have considered using innerHtml, but that does not get the value, it only gets what is between the <li> tags.

Any help would be appreciated!

Thanks!

<script type='text/javascript'>  

function ajaxAssignFull() {
var assignment_string = 
var assignment_string = document.getElementById('assignment_string_full').value
alert(assignment_string);   
}

</script>

<li id="assignment_string_full" onclick='ajaxAssignFull()' value="Jason_Example_12"><a 
href="#">Jason Example</a></li>

Upvotes: 0

Views: 4478

Answers (3)

Tamil Selvan C
Tamil Selvan C

Reputation: 20199

Try getAttribute('value')

function ajaxAssignFull() {
    var assignment_string = document.getElementById('assignment_string_full');
    alert(assignment_string.getAttribute('value'));
}

Edit:

Use data-attribute is a good way.

Upvotes: 0

anomaaly
anomaaly

Reputation: 841

document.getElementById('assignment_string_full').attributes.value.nodeValue

Upvotes: 0

PSL
PSL

Reputation: 123739

Value wil make your html invalid on li, as li does not have a value attribute. Instead try using html5 data-*. You can always use getAttribute('value') but be discouraged to do so since value is invalid on li.

Html

<li id="assignment_string_full" onclick='ajaxAssignFull()' data-value="Jason_Example_12"><a 
href="#">Jason Example</a></li>

Script

var assignment_string = document.getElementById('assignment_string_full').getAttribute('data-value');

Upvotes: 2

Related Questions