Welcome to the nuBuilder Forums!

Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.

select object selected text

Questions related to using nuBuilder Forte.
Post Reply
Timo
Posts: 221
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

select object selected text

Unread post by Timo »

I have a Select Object (ID: select_field) with multiple set to Yes. Before saving the record, I check if the selected option contains a specific text.
As soon as the text contains a space, indexOf() always returns -1.

This works when I pass a string without spaces to indexOf()

Code: Select all

var s = $('#select_field').find("option:selected").text(); // s = "123 Hello World"
var i = s.indexOf('Hello');
alert(i);
This doesn't work when I pass a string with spaces to indexOfO

Code: Select all

var s = $('#select_field').find("option:selected").text();  // s = "123 Hello World"
var i = s.indexOf('Hello World');
alert(i);
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: select object selected text

Unread post by toms »

Hi,

The text that is returned by .find("option:selected").text() contains the character 160 (Non-breaking space). Here's a fix to replace it with an ordinary space.

Code: Select all

function fixSpaces(s) {
  return s.replace(/\s/g /* all kinds of spaces*/," " /* ordinary space */);
}

var s = fixSpaces($('#select_field').find("option:selected").text());  
var i = s.indexOf('Hello World');
alert(i);
Timo
Posts: 221
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Re: select object selected text

Unread post by Timo »

You're a genius, thank you!
admin
Site Admin
Posts: 2829
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 30 times

Re: select object selected text

Unread post by admin »

.
Post Reply