• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Javascript question.. updating element

Hi guys,

I have the following element that I want to be able to change.

<span id="imageCount">5</span>

This displays a numeric count of images in a folder. When someone deletes/adds an image, I need to increase this via JS. (upon page refresh, it will be updated, but until then I need to update it).

Using the element ID of 'imageCount', what would be the best way to change it?

*This imagecount variable '5' or whatever is just going to be a string.. I haven't declared it as a numeric js variable. So before doing the increase/decrease (+1 or -1), I'd need to strip the contents out of that element and make it numeric..

Thoughts?

Code:
$('#imageCount' + i)

Code:
$('##imageCount').replaceWith('('##imageCount + 1)');
 
Last edited:
No need to take the overhead of creating a new element. Just keep the count in a separate var and use .text() on the element's wrapper to set it.
 
Code:
var imageCount = $("#imageCount").text();

function functionThatIsRunWheneverYouAddAnImage() {
    imageCount++;
    $("#imageCount").text(imageCount);
}

Just an elaboration of what Markbnj said. Get the current count on your page and into your js, then increment that variable and throw it back onto the page. Instead of pulling the count on page load into your js via .text() as I did here, you could also have your back end populate that between some script tags.

If you need to delete an image, imageCount--;
 
Back
Top