• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Can I include <script> inside a html tag ?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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:
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic