• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Can I include <script> inside a html tag ?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<input type="radio"
<script> document.write(getIfChecked()); </script>
/><label>City</label>


<script>
function getIfChecked() {
if(..) return "checked";
else return "";

}

</script>

Here I meant to create a radio input and use a javascript code to decide if I need to make it "checked".
But it does not work. How do I accomplish this ?


 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is happening when the page is rendering it should work.

Have you tried it? What errors are you getting? Why are you not doing it on the server?

Eric

 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't render correctly in any browser I tried.

The point is: this is not the way to do it.

As Eric said, if you can make this determination on the server, it should be handled there. If not, then you can add the attribute under script control on the client, but not with embedded script tags or document.write().

Using jQuery, it'd be as easy as:

Without jQuery, you'd use the native setAttribute() method.
 
FY Hsieh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really not sure how you can use

setAttribute()

to create the html dynamically. Could you just use my simple code snippet to show how that "checked" can be dynamically handled ?

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

FY Hsieh wrote:I am really not sure how you can use

setAttribute()

to create the html dynamically.


You can't use it to create general HTML, but you can certainly use it to add the checked attribute to an element.

Where is your code that's not working?
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem may be that you are defining the script method after you use it.

Try this


However, this a very crude method to get this behaviour. Do it on the server or use jquery.

With jquery:
 
reply
    Bookmark Topic Watch Topic
  • New Topic