user4329628 user4329628 - 1 year ago 82
jQuery Question

Populate a field based from value of body class value

Trying to populate a field in a form with a class from the body element, which starts "pageid-" and then has a number after it. I would like to find this number, and add it as the form value which enables me to use the same form on every page but identify which page the form was submitted on.

Still learning Java/Jquery so apologies on not knowing how to return the value here, it's coming in as [object Object] - This is what I have currently:


<body class="pageid-1232">

<input type="text" id="test1" value="">



var thisid = $('[class^="pageid-"]');

$(document).ready(function () {
$("#test1").val( thisid );

Answer Source

Try this:

var thisid = $('[class^="pageid-"]');

$(document).ready(function () {
    var num = $(thisid).attr("class").split("-").pop(); // it will extract the number from class 
    $("#test1").val( num ); // set that number to input field
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download