user4329628 user4329628 - 4 months ago 6x
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 );


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